[SQL 문제 풀이] Students and Examinations (학생 및 시험)

Stupefyee's avatar
Jun 10, 2025
[SQL 문제 풀이] Students and Examinations (학생 및 시험)
notion image
notion image
각 학생이 각 시험에 출석한 횟수를 구하기 위한 해결책을 작성하세요. student_id와 subject_name으로 정렬된 결과 테이블을 반환합니다.
 

내가 작성한 쿼리

MySQL, Oracle

SELECT ST.student_id, ST.student_name, SU.subject_name, COUNT(E.student_id) AS attended_exams FROM Students ST CROSS JOIN -- 학생 × 과목의 모든 조합을 만든 후 Subjects SU LEFT JOIN -- 해당 조합에 시험을 본 경우만 JOIN >> 안 본 경우 NULL 처리 Examinations E ON ST.student_id = E.student_id AND SU.subject_name = E.subject_name GROUP BY ST.student_id, ST.student_name, SU.subject_name ORDER BY ST.student_id, SU.subject_name;
 
Share article

stupefyee