0
点赞
收藏
分享

微信扫一扫

181. 超过经理收入的员工

王小沫 2022-02-03 阅读 24
linqsqlp2p

Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+
给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

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

----------------------------

解析:

方法1:自身连接

select E1.Name as Employee

from Employee E1,Employee E2

where E1.ManagerId=E2.Id and E1.Salary > E2.Salary

方法2:谓词exists

select E1.Name as Employee

from Employee E1

where exists

    ( select *

      from Employee E2

      where E1.ManagerId=E2.Id and E1.Salary > E2.Salary )

方法3:内查询>

select E1.Name as Employee

from Employee E1

where salary >

    ( select salary

      from Employee

      where id=E1.ManagerId )

方法4:内查询in

select E1.Name as Employee

from Employee E1

where ManagerId in

    ( select id

      from Employee

      where E1.salary > salary )

https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/

举报

相关推荐

0 条评论