LeetCode: Managers with at Least Direct Reports

LeetCode: Managers with at Least 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.

1
2
3
4
5
6
7
8
9
10
+------+----------+-----------+----------+
|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:

1
2
3
4
5
+-------+
| Name |
+-------+
| John |
+-------+

Note: No one would report to himself.

1
2
3
4
5
6
# Write your MySQL query statement below
SELECT a.Name
FROM Employee a, Employee b
Where a.Id = b.ManagerId
Group By a.Name
Having Count(*)>4