[LeetCode] 1280. Students and Examinations

Chobby·2025년 7월 30일
1

LeetCode

목록 보기
486/582

😎풀이

  1. 학생의 아이디, 시험 과목, 해당 시험 과목의 응시 수를 조회
  2. CROSS JOIN을 통해 Subject 테이블과 Students 테이블을 결합
  3. LEFT JOIN을 통해 Examinations 테이블과 기존 테이블을 결합
    3-1. 학생 아이디를 기준으로 결합
    3-2. 시험 과목을 기준으로 결합
  4. 학생 아이디, 학생 이름, 과목명을 기준으로 그룹화
  5. 학생 아이디, 과목명을 기준으로 오름차 순 정렬하여 반환환
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 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;
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글