Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[LeetCode] 570. Managers with at Least 5 Direct Reports

์ธ” 2023. 4. 24. 18:34

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ 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์„ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋‚˜๊ฐ€๋Š” ๋ฐฉํ–ฅ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์—ฌ์ „ํžˆ ๋‹ค์–‘ํ•˜๊ฒŒ ๋งŽ์ด ํ’€์–ด๋ณด๊ณ  ๊ธฐ๋กํ•ด์•ผํ•จ์„ ๋Š๊ผˆ๋‹ค.