Problem
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
|
|
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:
|
|
Note:
No one would report to himself.
Analysis
For this problem, we can group by ManagerId and find id’s of managers who have at least 5 direct reports:
|
|
Then output corresponding names:
|
|
Also, we can join 2 Employee tables to link employees and their managers, then group by managers and filter with number of employees reporting to them directly:
|
|
Solution
Solution 1
|
|
Solution 2
|
|
Link
570. Managers with at Least 5 Direct Reports
(中文版) SQL 笔记: Leetcode#570 Mnanagers with at Least 5 Direct Reports
近期评论