Employee 表包含所有员工和他们的经理。 每个员工都有一个 Id,并且还有一列是经理的 Id。
+------+----------+-----------+----------+
|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 |
+------+----------+-----------+----------+
给定 Employee 表,请编写一个SQL查询来查找至少有5名直接下属的经理。 对于上表,您的SQL查询应该返回:
+-------+
| Name |
+-------+
| John |
+-------+
注意:
没有人是自己的下属。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/managers-with-at-least-5-direct-reports 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
# Write your MySQL query statement below
select Name
from Employee
where Id in
(
select ManagerId
from Employee
group by ManagerId
having count(*) >= 5
)
or
# Write your MySQL query statement below
select e1.Name
from Employee e1 left join Employee e2
on e1.Id = e2.ManagerId
group by e1.Name
having(count(*) >= 5)