leetCode SQL 超過經理收入的員工

目錄

題目

解1:自連接

解2:子連接1

解3:子連接2


 

題目

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:子連接1

--子連接1
select b.Name as Employee from Employee a,(select * from Employee) b
where a.id=b.ManagerId 
and a.Salary<b.Salary

解3:子連接2

--子連接2
select e.Name as Employee
from employee e
where salary > (select salary from employee where Id = e.ManagerId)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章