Managers with at Least 5 Direct Reports

You can find the managers with at least five direct reports by using the following SQL query:

1
2
3
4
5
6
7
8
9
SELECT name
FROM Employee
WHERE id IN (
  SELECT managerId
  FROM Employee
  WHERE managerId IS NOT NULL
  GROUP BY managerId
  HAVING COUNT(*) >= 5
);

Explanation:

  • In the subquery, we first filter out rows where managerId is null, then group the results by managerId and use the HAVING clause to filter groups that have a count of 5 or more.
  • In the outer query, we select the names of employees whose IDs are in the result of the subquery, yielding the names of managers with at least five direct reports.