leetcode181-Employees Earning More Than Their Managers

来源:互联网 发布:淘宝篮球鞋进货渠道 编辑:程序博客网 时间:2024/05/21 05:24

问题描述:

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      |+----------+

雇员表记录了所有雇员的信息,包括他们的经理在内。每一个雇员都有一个Id,和他的经理的Id。

给定雇员表,编写一个SQL查询找出薪水大于经理的员工姓名。对于上表来说,Joe是唯一收入大于经理的员工。

问题求解:

1、自连接

select a.name as 'Employee' from Employee a, Employee bwhere a.ManagerId=b.Id and a.salary>b.salary

2、exists

select name as 'Employee' from Employee a where exists (select Id from Employee b where a.ManagerId=b.Id and a.salary>b.salary)或者:select name as 'Employee' from Employee a where exists (select name from Employee b where a.ManagerId=b.Id and a.salary>b.salary)

3、左外连接:left join

select a.name as 'Employee' from Employee a left joinEmployee b on a.ManagerId=b.Id where a.salary>b.salary

4、内连接:inner join

select a.name as 'Employee' from Employee a inner joinEmployee b on a.ManagerId=b.Id and a.salary>b.salary
0 0