
1280. Students and Examinations
각 학생들이 각 과목을 응시한 횟수를 반환하라.
Students
+------------+--------------+
| student_id | student_name |
+------------+--------------+
| 1 | Alice |
| 2 | Bob |
| 13 | John |
| 6 | Alex |
+------------+--------------+
Subjects
+--------------+
| subject_name |
+--------------+
| Math |
| Physics |
| Programming |
+--------------+
Examinations
+------------+--------------+
| student_id | subject_name |
+------------+--------------+
| 1 | Math |
| 1 | Physics |
| 1 | Programming |
| 2 | Programming |
| 1 | Physics |
| 1 | Math |
| 13 | Math |
| 13 | Programming |
| 13 | Physics |
| 2 | Math |
| 1 | Math |
+------------+--------------+
SELECT S.student_id,
S.student_name,
sub.subject_name,
COUNT(E.student_id) AS attended_exams
FROM Students S
CROSS JOIN Subjects sub
LEFT JOIN Examinations E
ON S.student_id = E.student_id
AND sub.subject_name = E.subject_name
GROUP BY S.student_id, S.student_name, sub.subject_name
ORDER BY S.student_id, sub.subject_name
LEFT JOIN으로 조인해야 student_id기준으로 정렬됨.LEFT JOIN Examinations E ON S.student_id = E.student_id
LEFT JOIN으로 조인해야 subject_name기준으로 정렬됨.LEFT JOIN Examinations E ... AND sub.subject_name = E.subject_name
하지만 여전히 어렵다...