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




Related Posts

Find Median Given Frequency Of Numbers Problem

LeetCode 571. Write a query to find the median of...

Find Cumulative Salary Of An Employee Problem

LeetCode 579. Write a SQL to get the cumulative sum...

Get Highest Answer Rate Question Problem

LeetCode 578. Write a sql query to identify the question...

Winning Candidate Problem

LeetCode 574. Write a sql to find the name of...

Managers With At Least 5 Direct Reports Problem

LeetCode 570. Given the Employee table, write a SQL query...

Employee Bonus Problem

LeetCode 577. Select all employee’s name and bonus whose bonus...