Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Seongho Jang

Leetcode 1280. Students and Examinations 본문

SQL

Leetcode 1280. Students and Examinations

seonghojang 2024. 3. 2. 22:48

https://leetcode.com/problems/students-and-examinations/?envType=study-plan-v2&envId=top-sql-50

 

 

Leetcode에 SQL 50이라는 게 있어서 풀어보고 있는데,

앞에 Easy 레벨들은 한번에 풀어야지~ 하고 풀다가 의외로 막힌 문제가 있어서 기록하려고 블로그를 켰다.

 

문제 자체는 엄청나게 간단해보였지만

학생이 응시하지 않은 시험들까지 전부 출력해야 해서 상당히 난감했던 문제

LEFT JOIN을 써도 저렇게 출력은 안되고,

CROSS JOIN을 해야 했다.

 

우선 Students 테이블과 Subjects 테이블을 Cross Join해서

가능한 모든 경우의 수를 만들어놓는다.

SELECT *
FROM Students s
CROSS JOIN Subjects j
ORDER BY s.student_id

 

여기까지 하면 틀은 깔끔하게 만들어지고,

 

| student_id | student_name | subject_name |
| -------------- | ------------------- | ------------------- |
| 1                | Alice                | Programming |
| 1                | Alice                | Physics          |
| 1                | Alice                | Math               |
| 2                | Bob                 | Programming |
| 2                | Bob                 | Physics           |
| 2                | Bob                 | Math               |
| 6                | Alex                 | Programming |
| 6                | Alex                 | Physics          |
| 6                | Alex                 | Math               |
| 13              | John                | Programming |
| 13              | John                | Physics          |
| 13              | John                | Math               |

 

Examinations 테이블을 Left Outer Join 하면 된다.

SELECT *
FROM Students s
CROSS JOIN Subjects j
LEFT JOIN Examinations e
ON s.student_id = e.student_id 
   AND j.subject_name = e.subject_name
ORDER BY s.student_id

단 Left Join의 조건이 j.subject_name = e.subject_name이 추가되어야 하는데,

만약 s.student_id = e.student_id의 조건만 주게 되면

아래와 같이 e.subject_name의 개수만큼 subject_name이 붙어버려 아무 의미없는 테이블이 되어버린다.

j.subject_name의 항목 개수는 뭐에 따라서 결정되는지 잘 모르겠다...

 

| student_id | student_name | subject_name | student_id | subject_name |
| -------------  | ------------------- | ------------------- | -------------- | ------------------ |
| 1                | Alice                | Programming  | 1                | Math |
| 1                | Alice                | Programming  | 1                | Math |
| 1                | Alice                | Math               | 1                | Math |
| 1                | Alice                | Math               | 1                | Physics |
| 1                | Alice                | Math               | 1                | Programming |
| 1                | Alice                | Math               | 1                | Physics |
| 1                | Alice                | Math               | 1                | Math |
| 1                | Alice                | Math               | 1                | Math |
| 1                | Alice                | Physics           | 1                | Math |
| 1                | Alice                | Physics           | 1                | Physics |
| 1                | Alice                | Physics           | 1                | Programming ...

 

SELECT s.student_id
     , s.student_name
     , j.subject_name
     , COUNT(e.subject_name) AS attended_exams
FROM Subjects j
CROSS JOIN Students s
LEFT JOIN Examinations e
ON s.student_id = e.student_id
   AND j.subject_name = e.subject_name
GROUP BY s.student_id
       , s.student_name
       , j.subject_name
ORDER BY s.student_id
       , j.subject_name

최종적으로 문제의 조건에 따라 COUNT함수를 넣고

적당히 정렬을 하면 끝인 문제

 

아직까지 trial and error로 풀고 있고, 명쾌하게 정답을 내지는 못하겠다.

아직도 갈길이 멀다~