欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LeetCode--181.超过经理收入的员工

程序员文章站 2022-03-13 21:25:37
...

LeetCode--181.超过经理收入的员工

建表

Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, ManagerId int);
Truncate table Employee;
insert into Employee (Id, Name, Salary,ManagerId) values ('1', 'Joe', '70000', '3');
insert into Employee (Id, Name, Salary,ManagerId) values ('2', 'Henry', '80000', '4');
insert into Employee (Id, Name, Salary,ManagerId) values ('3', 'Sam', '60000', Null);
insert into Employee (Id, Name, Salary,ManagerId) values ('4', 'Max', '90000', Null);

表自身关联查询即可

select  e1.Id, e1.Name, e1.Salary
 from employee e1, employee e2
 where e1.ManagerId = e2.Id
 and e1.Salary > e2.Salary

子查询做法

select name Employee from employee e1
where e1.Salary > (select e2.Salary from employee e2 where e1.ManagerId = e2.Id)
相关标签: Leetcode