I have three tables.
Students
student_id | name
1 Rhon
Subjects
subject_id | subject_name | student_id
1 Physics 1
2 Math 1
Grades
grade_id | student_id | subject_id | grade
1 1 1 90
2 1 2 89
3 1 2 88
I would like to result to be like this:
student_id | student_name | subject_name | grades
1 Rhon Physics 90
1 Rhon Math 88,89
My current query is:
SELECT students.student_id, subjects.subject_id, string_agg(grades.grade, ',')
FROM students
JOIN subjects ON students.student_id = subjects.student_id
JOIN grades ON subjects.subject_id = grades.subject_id;
Is there something wrong with my query? Am I missing something? The error says that student_id
needs to be in a GROUP BY clause but I don't want that.