SQL problem - Managers with at Least 5 Direct Reports
Problem
Query
SELECT
e1.name
FROM
Employee AS e1
JOIN
Employee AS e2
WHERE
e1.id = e2.managerId
GROUP BY
e1.id
HAVING COUNT(*) >= 5
Editorial Solution
Approach 1: Group By and Join
SELECT
Name
FROM
Employee AS t1
JOIN
(SELECT
ManagerId
FROM
Employee
GROUP BY ManagerId
HAVING COUNT(ManagerId) >= 5) AS t2
ON
t1.Id = t2.ManagerId
;
Approach 2: IN Clause with Subquery
SELECT
name
FROM
employee
WHERE
id IN (
SELECT
managerId
FROM
employee
GROUP BY
managerId
HAVING COUNT(*) >= 5
);