๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/managers-with-at-least-5-direct-reports/description/
Managers with at Least 5 Direct Reports - LeetCode
Can you solve this real interview question? Managers with at Least 5 Direct Reports - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Table : Employee
Write an SQL query to report the managers with at least five direct reports.
Return the result table in any order.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
์ต์ 5๊ฐ์ ๋ณด๊ณ ์๋ฅผ ๋ณด๊ณ ๋ฐ๋ ๊ด๋ฆฌ์ ์ด๋ฆ์ ์ถ๋ ฅํด์ผ ํ๊ณ
์ ๋ ฌ ์์๋ ์์ ๋ฐํํ๋ค.
๊ฐ๋จํ๊ฒ ์๊ฐํ๋ฉด, ๊ฐ๊ฐ์ employee์ ๋ํ manager๋ฅผ ํ์ธํ๊ณ manager 1๋ช ๋น ์ฐ๊ฒฐ๋ employee๊ฐ 5๋ช ์ด์์ธ manager ์ด๋ฆ์ ์ถ๋ ฅํ๋ฉด ๋๋ค.
๋ฐ๋ผ์ Employee ํ ์ด๋ธ์ ์ ํ ์กฐ์ธํด์ ์ง๊ณ ํจ์ ์ ์ฉ ๊ธฐ์ค์ ๊ทผ๋ก์ ์ ๋ณด(id๋ name)๊ฐ ์๋ manager(id, name) ๊ธฐ์ค์ผ๋ก ๋๋ค.
SELECT e2.name AS name
FROM Employee e1
JOIN Employee e2
ON e1.managerId = e2.id
GROUP BY e2.name
HAVING COUNT(*) >= 5
โ ๊ฐ์ธํ๊ณ
์ฒ์์ ๋ฌด์ ๋๋ฌธ์ธ์ง group by์ having์ ์ ์ ์ฉํ๋ ๊ฒ์ ์๊ฐ์ด ๋ฉ์ถฐ์ with์ ์ ์ฌ์ฉํด์ ๊ฒฐ๊ณผ๊ฐ์ ์ถ๋ ฅํ๋ค.
์ฐ๊ณ ๋ณด๋ with์ ๋ก ์์ํ ์ด๋ธ์ ๋ง๋ค ํ์๊น์ง๋ ์์๋ค. ์กฐ์ธํ ํ ์ด๋ธ์ ๋๊ณ manager๊ธฐ์ค์ผ๋ก ์ง๊ณํ๋ ๊ฑธ ์๊ฐํ๋ ๊ณผ์ ์ด ์๊ฐ๋ณด๋ค ์ค๋ ๊ฑธ๋ ธ๋ค.
์ค๋๋ sql์ ๋ฌธ์ ๋ฅผ ํ์ด๋๊ฐ๋ ๋ฐฉํฅ๋๋ก ์ฌ์ฉํ๋ ค๋ฉด ์ฌ์ ํ ๋ค์ํ๊ฒ ๋ง์ด ํ์ด๋ณด๊ณ ๊ธฐ๋กํด์ผํจ์ ๋๊ผ๋ค.
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 577. Employee Bonus (0) | 2023.05.10 |
---|---|
[LeetCode] 585. Investments in 2016 (0) | 2023.05.09 |
[LeetCode] 550. Game Play Analysis IV (0) | 2023.04.21 |
[LeetCode] 607. Sales Person (0) | 2023.04.20 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์กฐ๊ฑด์ ๋ง๋ ์ฌ์ฉ์์ ์ด ๊ฑฐ๋๊ธ์ก ์กฐํํ๊ธฐ (0) | 2023.04.07 |