[LeetCode] 1280. Students and Examinations

2023. 12. 1. 17:05·코딩테스트/MySQL
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

  1. CROSS JOIN은 곱집합으로, 모든 경우의 수를 구하는 것과 같은 결과이다.
  2. 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
'코딩테스트/MySQL' 카테고리의 다른 글
  • [LeetCode] 178. Rank Scores
  • [LeetCode] 1174. Immediate Food Delivery II
  • [Programmers] Level 2. 가격대 별 상품 개수 구하기
  • [Programmers] Level 2. 조건별로 분류하여 주문상태 출력하기
swwho
swwho
일상을 데이터화하다
  • swwho
    하루한장
    swwho
  • 전체
    오늘
    어제
    • 분류 전체보기 (188)
      • ML_DL (39)
        • MUJAKJUNG (무작정 시리즈) (18)
        • 딥러닝 공부하기 (21)
      • 데이터사이언스 (1)
        • EDA (1)
        • 데이터과학을 위한 통계 (0)
      • 데이터엔지니어링 (2)
      • 논문리뷰 (2)
        • Computer Vision (2)
      • Python 활용하기 (12)
      • 코딩테스트 (127)
        • Python (109)
        • MySQL (14)
      • Git (3)
      • MySQL 활용하기 (0)
      • 일상 이야기 (1)
  • 블로그 메뉴

    • 홈
    • 태그
  • 최근 글

  • 250x250
  • hELLO· Designed By정상우.v4.10.3
swwho
[LeetCode] 1280. Students and Examinations
상단으로

티스토리툴바