๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ๋ฌธ์
https://leetcode.com/problems/students-and-examinations/description/
Table : Students
Table : Subjects
Table : Examinations
Write a solution to find the number of times each student attended each exam.
Return the result table ordered by student_id and subject_name.
…
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
ํ์ ์ ๋ณด(student_id, student_name)
๊ณผ๋ชฉ ์ ๋ณด(subject_name)
ํ์๋ค์ ์ํ ์์ ์ ๋ณด(student_id, subject_name) ๋ฅผ ์กฐ์ธํด์ '๋ชจ๋ ํ์๋ค'์ ์ํ ์์ ํ์(์์ ์ด๋ ฅ็ก)๋ฅผ ์ถ๋ ฅํด์ผ ํ๋ค.
์กฐ์ธํ ๋ ์ค์ํ ๊ฒ์ ๊ฐ ํ ์ด๋ธ ๊ฐ ๋ฒ์๋ฅผ ์ ํ์ ํ๋ ๊ฒ์ด์๋ค.
(๋ฌธ์ ์์ ์ ๊ณตํ๋ ๊ฐ ํ ์ด๋ธ์ primary key๋ฅผ ์ ํ์ ํ๋ ๊ฒ์ด ์กฐ์ธ ๊ด๊ณ๋ฅผ ํ์ฑํ ๋ ๋์์ด ๋๋ค.)
์)
Students ํ ์ด๋ธ์ ํ์์๋ Examinations ์ ํ์์๋ณด๋ค ๋ง์ ์ ์๋ค.(=์ํ ์์ ์ด๋ ฅ์ด ์๋ ํ์ ์กด์ฌ)
Subjects ํ ์ด๋ธ์ ๊ณผ๋ชฉ์ข ๋ฅ๋ Examinations ์์ ๊ฐ ํ์์ด ์์ํ ๊ณผ๋ชฉ์๋ณด๋ค ๋ง์ ์ ์๋ค. (=์ํ ๊ณผ๋ชฉ์ค ์์ ์ํ ๊ณผ๋ชฉ์ด ์๋ ํ์ ์กด์ฌ)
์ด๋ฌํ ๊ฒฝ์ฐ๋ฅผ ์๊ฐํด์ ํ์, ์ํ๊ณผ๋ชฉ, ์์์ด๋ ฅ์ด ๋๋ฝ๋์ง ์๋๋ก ์๋์ ๊ฐ์ด ์กฐ์ธํ๋ค.
SELECT stu.student_id, stu.student_name, sub.subject_name,
COUNT(exam.subject_name) AS attended_exams
FROM Subjects sub
INNER JOIN Students stu
LEFT JOIN Examinations exam
ON stu.student_id = exam.student_id
AND sub.subject_name = exam.subject_name
GROUP BY 1, 3
ORDER BY 1, 3;
๐โ๏ธ ๋ฌธ์ ํ์ด์ mysql ์ฟผ๋ฆฌ ํจ์จ์ฑ ๋์ด๊ธฐ ๋ฑ ํฌ์คํ ์ ๋ํ ์๊ฒฌ ๋ฐ ๊ธฐ์ ์กฐ์ธ ๋๊ธ ๋ชจ๋ ํ์ํฉ๋๋ค.
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leet Code] 1407. Top Travellers (1) | 2023.10.20 |
---|---|
[Leet Code] 1251. Average Selling Price (0) | 2023.10.18 |
[Leet Code] 1965. Employees With Missing Information (0) | 2023.10.12 |
[Leet code] 1978. Employees Whose Manager Left the Company (0) | 2023.10.11 |
[Leet code] 1084. Sales Analysis III (0) | 2023.10.10 |