Employee
表包含全部員工,包括他們的經理。每一個員工都有一個 Id,此外還有一列對應的經理Id。spa
建立表和數據:3d
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);
解法:code
1.經過表的自鏈接,找出每一個員工的經理,篩選出薪水比經理薪水高的員工。blog
select E1.Name as Employee from Employee as E1 join Employee as E2 on (E1.ManagerId = E2.Id and E1.salary > E2.salary)