Managers with at Least 5 Direct Reports

수이·2025년 5월 27일

🟢 코드카타 / SQL

목록 보기
88/88

Write a solution to find managers with at least five direct reports.
Return the result table in any order.
The result format is in the following example.
문제링크

조건정리
1. 테이블이 하나여서 셀프조인 > 매니저랑 부하직원을 각각 조회해야 되니까
2. 5명 이상의 부하 직원이 있을 경우만 필터링

풀이

SELECT a.name
FROM employee a
JOIN employee b ON a.id = b.managerId
GROUP BY a.id, a.name
HAVING COUNT(*) >= 5

다른사람 풀이

SELECT T101.NAME
FROM Employee T101
WHERE T101.id IN (
		SELECT DISTINCT (T102.managerId)
		FROM Employee T102
		GROUP BY T102.managerId
		HAVING COUNT(T102.id) > 4
		)

난 이렇게 IN 쓰는 방식이 바로바로 안 떠오르는 것 같다 😅

0개의 댓글