SQL-one question per day [570. Managers with at least 5 direct reports]

topic

surface: Employee

Write an SQL query for managers who have at least 5 direct  reports .

Return the result table in  any order  .

The query result format is as follows.

Example 1:

problem solving ideas

1. To query managers with at least 5 direct subordinates  , we first classify the manageId

group by manageId

2. Then find out the manageId whose number of managedId after classification is greater than or equal to 5

group by ManagerId
    having count(ManagerId)>=5

3. Use where to query the ids that exist in the above search results, and query the corresponding names.

Code

select Name 
from Employee
where id in(
    select distinct ManagerId
    from Employee
    group by ManagerId
    having count(ManagerId)>=5
)

Test Results

 

Guess you like

Origin blog.csdn.net/lucky_1314520/article/details/131599859