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

2 超过经理收入的员工 #3

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

2 超过经理收入的员工 #3

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

Comments

@astak16
Copy link
Owner

astak16 commented Jan 3, 2022

题目

经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。查询收入超过他们经理的员工的姓名

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

insert into employee values 
(1, 'Joe', 70000, 3),
(2, 'Henry', 80000, 4),
(3, 'Sam', 60000, null),
(4, 'Max', 90000, null);

SQL

select employee.name from employee left join employee e
on employee.managerId = e.id
where employee.salary > e.salary;

解析

managerId 是经理 id ,同时经理也是员工,也就是说没有 managerId 是普通员工,有 managerId 的是经理。

所以将 employee 自连接,连接条件是 employee.managerId = e.id ,就可以把普通员工和经理连接起来了。

然后在筛选出 employee.salary > e.salary 的员工就行了。

@astak16 astak16 added the 简单 label Jan 3, 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