728x90
반응형
🔗 Problem Link
Students and Examinations - LeetCode
Can you solve this real interview question? Students and Examinations - Table: Students +---------------+---------+ | Column Name | Type | +---------------+---------+ | student_id | int | | student_name | varchar | +---------------+---------+ student_id is
leetcode.com
❔Thinking
- 학생 개개인의 과목별 시험 본 횟수를 알아내야 한다.
- 시험을 보지 않은 과목은 0번 본 것으로 생각해야 한다.
💻Solution
SELECT St.student_id, St.student_name, Su.subject_name, COUNT(Ex.student_id) AS attended_exams
FROM Students AS St
CROSS JOIN Subjects AS Su
LEFT JOIN Examinations AS Ex ON St.student_id = Ex.student_id AND Su.subject_name = Ex.subject_name
GROUP BY St.student_id, Su.subject_name
ORDER BY St.student_id, Su.subject_name
🗝️keypoint
- CROSS JOIN은 곱집합으로, 모든 경우의 수를 구하는 것과 같은 결과이다.
- COUNT 함수는 정확한 GROUP BY와 함께 쓰여야 한다.
'코딩테스트 > MySQL' 카테고리의 다른 글
[LeetCode] 178. Rank Scores (0) | 2025.04.11 |
---|---|
[LeetCode] 1174. Immediate Food Delivery II (1) | 2023.12.04 |
[Programmers] Level 2. 가격대 별 상품 개수 구하기 (0) | 2023.01.03 |
[Programmers] Level 2. 조건별로 분류하여 주문상태 출력하기 (0) | 2022.12.26 |
[Programmers] Level 2. 진료과별 총 예약 횟수 출력하기 (0) | 2022.11.30 |