【SQL】570. 至少有5名直接下属的经理 (构建临时表tmp)

【SQL】570. 至少有5名直接下属的经理 (构建临时表tmp)_第1张图片
【SQL】570. 至少有5名直接下属的经理 (构建临时表tmp)_第2张图片

写法一

select a.name
from Employee a
where a.id = (
    select b.managerId
    from Employee b
    where a.id = b.managerId
    group by b.managerId
    having COUNT(*) >= 5
    );

写法二,构建临时表tmp

select a.name
from Employee as a
join (
    select managerId
    from Employee
    group by managerId
    having COUNT(*) >= 5
) as tmp
on a.id = tmp.managerId;

你可能感兴趣的:(SQL,Leetcode,sql,数据库,mysql)