LeetCode超過經理收入的員工SQL
阿新 • • 發佈:2020-12-22
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 | +----------+
解法一:
# Write your MySQL query statement below select Name as Employee from Employee as a where Salary > (select Salary from Employee where Id = a.ManagerId)
解法二:將兩個表做連線,比解法一效率高
# Write your MySQL query statement below select a.Nameas Employee from Employee as a join Employee as b on a.ManagerId = b.Id and a.Salary > b.Salary