#87. [LeetCode / MySQL] 570. Managers with at Least 5 Direct Reports

문제

 

입력 코드

1. SELF JOIN

SELECT e1.name
FROM Employee AS e1
JOIN Employee AS e2
    ON e1.id = e2.managerId
GROUP BY e1.id
HAVING count(e2.managerId) >= 5;

 

2. SUBQUERY

SELECT name 
FROM Employee
WHERE id IN (
    SELECT managerId
    FROM Employee
    GROUP BY managerId
    HAVING COUNT(managerId) >= 5
)

 

코드 설명

#SELECT #JOIN #SELF JOIN #GROUP BY #HAVING #COUNT #SUBQUERY #IN

 

 

문제 출처

 

Managers with at Least 5 Direct Reports - LeetCode

Can you solve this real interview question? Managers with at Least 5 Direct Reports - Table: Employee +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | | department | varchar | | managerId | int | +-

leetcode.com