Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17 至少有五名直接下属的经理 #22

Open
astak16 opened this issue Jan 12, 2022 · 0 comments
Open

17 至少有五名直接下属的经理 #22

astak16 opened this issue Jan 12, 2022 · 0 comments
Labels

Comments

@astak16
Copy link
Owner

astak16 commented Jan 12, 2022

题目

查找至少有 5 名直接下属的经理

create table employee (
	id int,
	name varchar(255),
	department varchar(255),
	managerId int
);

insert into employee values
(101, 'John', 'A', null),
(102, 'Dan', 'A', 101),
(103, 'James', 'A', 101),
(104, 'Amy', 'A', 101),
(105, 'Anne', 'A', 101),
(106, 'Ron', 'B', 101);

SQL:方法一

select name from employee where id in (
	select managerId from employee group by managerId having count(managerId) >= 5
);

解析

  1. 按照 managerId 分组,使用 having 筛选出大于等于 5 名下属的经理 id
select managerId from employee group by managerId having count(managerId) >= 5
  1. 使用 in 查出经理名字

SQL:方法二

select name from employee, (
	select managerId from employee group by managerId having count(managerId) >= 5
) as temp where employee.id = temp.managerId;

解析

  1. 按照 managerId 分组,使用 having 筛选出大于等于 5 名下属的经理 id,作为临时表
select managerId from employee group by managerId having count(managerId) >= 5
  1. 连查两张表,筛选条件是 employee.id = temp.managerId
@astak16 astak16 added the 中等 label Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant