Skip to content

Latest commit

 

History

History
 
 

570. Managers with at Least 5 Direct Reports

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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  |
+-------+

Note:
No one would report to himself.

Companies:
Bloomberg

Solution 1.

# OJ: https://leetcode.com/problems/managers-with-at-least-5-direct-reports/
# Author: github.com/lzl124631x
SELECT Name
FROM Employee AS a JOIN
    (SELECT ManagerId
    FROM Employee
    GROUP BY ManagerId
    HAVING Count(ManagerId) >= 5) as b
    ON a.Id = b.ManagerId