Skip to content

Managers with at Least 5 Direct Reports – SQL Solution

--570. Managers with at Least 5 Direct Reports
--The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+------+----------+-----------+----------+
|Id    |Name 	  |Department |ManagerId |
+------+----------+-----------+----------+
|101   |John 	  |A 	      |null      |
|102   |Dan 	  |A 	      |101       |
|103   |James 	  |A 	      |101       |
|104   |Amy 	  |A 	      |101       |
|105   |Anne 	  |A 	      |101       |
|106   |Ron 	  |B 	      |101       |
+------+----------+-----------+----------+
--Given the Employee table, write a SQL query that finds out managers with at least 5 direct report. For the above table, your SQL query should return:

+-------+
| Name  |
+-------+
| John  |
+-------+

SELECT e1.Name
FROM Employee as e1 
     JOIN
     (SELECT ManagerId
      FROM Employee
      GROUP BY ManagerId
      HAVING COUNT(ManagerId) >= 5) AS e2
     ON t1.Id = t2.ManagerId
See also  Calculate the average of an array of numbers in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.