SQL/리트코드 문제풀이

[LeetCode] 570 Managers with at Least 5 Direct Reports

너지살 2024. 5. 22. 14:33

 

 

 

문제 출저

https://leetcode.com/problems/managers-with-at-least-5-direct-reports/description/?envType=study-plan-v2&envId=top-sql-50

 

 

 

 

문제 풀이

5명 이상의 직속 부하가 있는 직원의 이름을 출력해야 합니다. 

Employee 테이블을 id와 managerId로 조인합니다.

manager.id를 그룹화하고 having으로 5개 이상을 선택합니다. 

 

 

 

SQL

-- # 5명 이상의 직속 부하가 있는 직원의 이름을 출력한다. 이름이 중복될 수 있다. 

select 
    manager.name 
from 
    Employee as manager
join
    Employee as employee on manager.id = employee.managerId 
group by
    manager.id 
having
    count(manager.id) >= 5 



-- with temp as (
--     select 
--         boss.id,
--         boss.name as boss_name,
--         sub.name as sub_name
--     from
--         Employee as boss
--     join 
--         Employee as sub on boss.id = sub.managerId
-- )

-- select 
--     boss_name as name
-- from
--     temp
-- group by
--     id
-- having 
--     count(*) >= 5