表:Employee
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | | salary | int | | managerId | int | +-------------+---------+ Id是该表的主键。 该表的每一行都表示雇员的ID、姓名、工资和经理的ID。
编写一个SQL查询来查找收入比经理高的员工。
以 任意顺序 返回结果表。
查询结果格式如下所示。
示例 1:
输入: Employee 表: +----+-------+--------+-----------+ | id | name | salary | managerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | Null | | 4 | Max | 90000 | Null | +----+-------+--------+-----------+ 输出: +----------+ | Employee | +----------+ | Joe | +----------+ 解释: Joe 是唯一挣得比经理多的雇员。
解题思路:每个员工可能既是员工也可能是经理,所以将每个员工的经理的薪资找出来。再判断员工薪资大于经理薪资。
利用left join on 进行多表查询
select e1.name as Employee, e1.salary as EmployeeSalary ,
e2.name as manager,e2.salary as managerSalary
from Employee e1 left join Employee e2 on e1.managerId = e2.id
在此表的基础上查询员工薪资大于经理薪资的字段
select Employee from
(select e1.name as Employee, e1.salary as EmployeeSalary ,e2.name as manager,
e2.salary as managerSalary
from Employee e1 left join Employee e2 on e1.managerId = e2.id) e3
where EmployeeSalary > managerSalary