570.至少有5名直接下属的经理
| 序号 | 题目 | 难度 |
|---|---|---|
| 570 | 题目链接 | 中等 |
题目大意
编写一个解决方案,找出至少有五个直接下属的经理。
以 任意顺序 返回结果表。
表: Employee
| Column Name | Type |
|---|---|
| id | int |
| name | varchar |
| department | varchar |
| managerId | int |
id 是此表的主键(具有唯一值的列)。 该表的每一行表示雇员的名字、他们的部门和他们的经理的id。 如果managerId为空,则该员工没有经理。 没有员工会成为自己的管理者。
示例
输入
Employee 表:
| 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 |
输出
| name |
|---|
| John |
参考答案
with t1 as (
select
managerId
,count(1) as cnt
from Employee
where managerId is not null
group by 1
having cnt >=5
)
select
t2.name
from t1
join Employee t2
on t1.managerId = t2.id
;