hyezdata 님의 블로그

[SQL] Students and Examinations 본문

내일배움캠프/코드카타

[SQL] Students and Examinations

hyezdata 2025. 5. 9. 09:32
# 학생들이 각 과목마다 시험 본 횟수
# 학생들 id랑 과목명 순으로

select s.student_id, s.student_name, sub.subject_name, count(e.subject_name) as attended_exams
from Students s cross join Subjects sub left join Examinations e 
    on s.student_id=e.student_id and e.subject_name = sub.subject_name
group by student_name, sub.subject_name
order by s.student_id, sub.subject_name

 

★ 먼저, CROSS JOIN 으로 빈 과목들 채워주는 거 잊지 말기

★ select 절에서 어떤 테이블의 변수를 써야하는지 잘 선택 (아래 사진 참고)

 

https://leetcode.com/problems/students-and-examinations/description/

728x90
반응형