leetcode Employees Earning More Than Their Managers题解

题目描述:

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+

Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

+----------+
| Employee |
+----------+
| Joe      |
+----------+

中文理解:找到工资比其经理高的员工姓名。

解题思路:将Employee 和自身join,在一个临时表中得到经理的工资,若员工工资大于经理工资则输出。

代码(sql):

select t1.Name as Employee
from Employee t1
join
(
    select Id,Name,Salary,ManagerId
    from Employee
) t2
on t1.ManagerId=t2.Id
where t1.Salary> t2.Salary;

 

上一篇:Python上下文管理器(Context managers)


下一篇:Loomis Sayles欢迎Marques Benton担任首席多元、公平与包容官