其他分享
首页 > 其他分享> > leetcode181 超过经理收入的员工 Employees Earning More Than Their Managers

leetcode181 超过经理收入的员工 Employees Earning More Than Their Managers

作者:互联网

Employee表包含所有员工,包括他们的经理。每个员工都有一个 Id,此外还有一列对应的经理Id。

创建表和数据:

drop table Employee
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);

解法:

1.通过表的自连接,找出每个员工的经理,筛选出薪水比经理薪水高的员工。

select E1.Name as Employee
from Employee as E1 join Employee as E2 on (E1.ManagerId  = E2.Id and E1.salary > E2.salary)

 

标签:Salary,Managers,Name,ManagerId,Their,Employee,Id,E1,Earning
来源: https://www.cnblogs.com/forever-fortunate/p/11722830.html