Employee
表包含所有员工,包括他们的经理。每个员工都有一个 Id,此外还有一列对应的经理Id。
创建表和数据:
drop table EmployeeCreate 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)
原文地址:https://www.cnblogs.com/forever-fortunate/p/11722830.html
时间: 2024-11-05 19:33:54