Managers With At Least 5 Direct Reports Problem
Description
LeetCode Problem 570.
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 |
+-------+
MySQL Solution
1
2
3
4
5
6
7
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
LeetCode Database - Medium
LeetCode 177
LeetCode 178
LeetCode 180
LeetCode 184
LeetCode 534
LeetCode 550
LeetCode 570
LeetCode 574
LeetCode 578
LeetCode 580
LeetCode 585
LeetCode 602
LeetCode 608
LeetCode 612
LeetCode 614
LeetCode 626
LeetCode 1045
LeetCode 1070
LeetCode 1077
LeetCode 1098
LeetCode 1107
LeetCode 1112
LeetCode 1126
More LeetCode Database
MySQL Tutorials