Need help creating a SQL query to display all courses a student has taken

Hey everyone! I’m stuck on a SQL problem and could use some advice. I’ve got three tables: Students, Classes, and Enrollments. I want to make a query that shows each student once, along with a list of all the classes they’ve taken.

Here’s what my tables look like:

Students:
StudentID | Name | Address | etc.

Classes:
ClassID | ClassName

Enrollments:
ClassID | StudentID

I’ve tried using JOINs and GROUP BY, but I can’t get it to work right. I want the output to look something like this:

1: Jane Doe
123 College St

Math 101
English 201
History 301

Any ideas on how to write this query? I’m using MySQL if that helps. Thanks in advance for any tips!

Hey Liam39! That’s a cool problem you’re working on. I love a good SQL challenge! :smile:

Have you tried using GROUP_CONCAT? It’s super handy for situations like this. Here’s a query that might do the trick:

SELECT
S.StudentID,
S.Name,
S.Address,
GROUP_CONCAT(C.ClassName ORDER BY C.ClassName SEPARATOR ‘\n’) AS Classes
FROM
Students S
LEFT JOIN
Enrollments E ON S.StudentID = E.StudentID
LEFT JOIN
Classes C ON E.ClassID = C.ClassID
GROUP BY
S.StudentID, S.Name, S.Address;

I used LEFT JOINs to make sure we get all students, even if they haven’t taken any classes yet. The GROUP_CONCAT part is the magic that lists all the classes in one column.

What do you think? Does this work for what you need? Let me know if you run into any issues or if you want to tweak it further. SQL can be tricky, but it’s so satisfying when you get it right! :raised_hands:

Oh, and I’m curious - what are you building with this data? Sounds like an interesting project!

hey liam, i think i got a solution for ya. try this query:\n\nSELECT S.StudentID, S.Name, S.Address,\nGROUP_CONCAT(C.ClassName SEPARATOR ‘\n’) AS Classes\nFROM Students S\nJOIN Enrollments E ON S.StudentID = E.StudentID\nJOIN Classes C ON E.ClassID = C.ClassID\nGROUP BY S.StudentID, S.Name, S.Address;\n\nthis should give u what ur looking for. lemme know if it works!

I’ve been working with SQL queries like this quite a bit recently, and I think I can offer a solution that might work for you. Here’s a query that should do the trick:

SELECT
S.StudentID,
S.Name,
S.Address,
GROUP_CONCAT(C.ClassName ORDER BY C.ClassName SEPARATOR ‘\n’) AS Classes
FROM
Students S
LEFT JOIN
Enrollments E ON S.StudentID = E.StudentID
LEFT JOIN
Classes C ON E.ClassID = C.ClassID
GROUP BY
S.StudentID, S.Name, S.Address;

This query uses LEFT JOINs to include all students, even those who haven’t taken any classes yet. The GROUP_CONCAT function with ORDER BY will list all classes alphabetically.

One thing to keep in mind: if you have a large dataset, you might want to consider indexing the StudentID and ClassID columns for better performance. Also, depending on your specific needs, you could modify the output format using CONCAT or other string functions.

Let me know if you need any clarification or if you run into any issues implementing this.