Overview

Sometimes when tables have a one-to-many relationship or many-to-many relationship we need to create a report of comma separated strings along with the necessary primary information.

Here I will explain a scenario in which a student can join multiple courses. To do this I am creating three tables, two master and one transaction, tables named Student, Course and trnjCourse_Studnet. The Student table has student information. The Course table has course name and course ID. The transaction table contains the information about a student and courses is a one-to-many relationship.

Now I need to write a query for student joining multiple courses, then the entire course name that is joined by that student should be displayed, separated by a comma with the unique student information. Let's see how to do it.

The Scripts

The following are the scripts to create the table.

  1. Student
    1. CREATE TABLE [dbo].[Student](
    2. [StudentId] [int] IDENTITY(1,1) NOT NULL,
    3. [StudentName] [varchar](50) NOT NULL,
    4. [FatherName] [varchar](50) NOT NULL,
    5. [Address] [varchar](100) NOT NULL,
    6. [MobileNo] [varchar](11) NOT NULL,
    7. CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
    8. (
    9. [StudentId] ASC
    10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    11. ) ON [PRIMARY]
  2. Course
    1. CREATE TABLE [dbo].[Course](
    2. [CourseId] [int] IDENTITY(1,1) NOT NULL,
    3. [courseName] [varchar](50) NOT NULL,
    4. CONSTRAINT [PK_Course] PRIMARY KEY CLUSTERED
    5. (
    6. [CourseId] ASC
    7. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    8. ) ON [PRIMARY]
  3. Transecting table

    The following is the Transecting table that contains the data about the student course list (trnjCourse_Studnet).
    1. CREATE TABLE [dbo].[trnjCourse_Studnet](
    2. [Id] [int] IDENTITY(1,1) NOT NULL,
    3. [StudentId] [int] NOT NULL,
    4. [CourseId] [int] NOT NULL,
    5. CONSTRAINT [PK_trnjCourse_Studnet] PRIMARY KEY CLUSTERED
    6. (
    7. [Id] ASC
    8. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    9. ) ON [PRIMARY]
    10. GO
    11. ALTER TABLE [dbo].[trnjCourse_Studnet] WITH CHECK ADD CONSTRAINT [FK_CourseId] FOREIGN KEY([CourseId])
    12. REFERENCES [dbo].[Course] ([CourseId])
    13. GO
    14. ALTER TABLE [dbo].[trnjCourse_Studnet] CHECK CONSTRAINT [FK_CourseId]
    15. GO
    16. ALTER TABLE [dbo].[trnjCourse_Studnet] WITH CHECK ADD CONSTRAINT [FK_StudentId] FOREIGN KEY([StudentId])
    17. REFERENCES [dbo].[Student] ([StudentId])
    18. GO
    19. ALTER TABLE [dbo].[trnjCourse_Studnet] CHECK CONSTRAINT [FK_StudentId]
    20. GO

Insert some data

Insert data in sql

After inserting the data, our main intention is to generate an output that contains student info along with the course name. If the student joins more than one course then the course name should display separated by commas as displayed in the following image.

student info

For getting this output I am writing a query using CTE.

  1. WITH Courseslist (StudentId,StudentName,FatherName,MobileNo,CourseName ) as
  2. (
  3. select trnjCourse_Studnet.StudentId,StudentName ,FatherName,MobileNo,Coursename from dbo.trnjCourse_Studnet
  4. left outer join Course
  5. on Course.Courseid =trnjCourse_Studnet.Courseid
  6. left outer join
  7. Student
  8. on trnjCourse_Studnet.StudentId =Student .StudentId
  9. ),
  10. CourseListForStudent as(
  11. SELECT StudentId ,StudentName,FatherName,MobileNo, LEFT(courseList , LEN(courseList)-1) AS CourseNameList
  12. FROM Courseslist AS extern
  13. CROSS APPLY
  14. (
  15. SELECT CourseName + ' , '
  16. FROM Courseslist AS intern
  17. WHERE extern.StudentId = intern.StudentId
  18. FOR XML PATH('')
  19. )pre_trimmed (courseList)
  20. Group by StudentId,courseList,StudentName,FatherName,MobileNo
  21. )
  22. SELECT * FROM CourseListForStudent
Summary

This article showed how to get comma separate a string in a SQL Server table with a 1 to many relationship with a table.

Thanks.

I would like to have feedback from my readers. Please post your feedback, question, or comments about this article.