【leetcode Database】181. 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表的兩個別名e1和e2,然後select e1的ManagerId字段與e2的Id字段相等,且e1的Salary大於e2的Salary。代碼如下:

# Write your MySQL query statement below
SELECT e1.Name AS Employee FROM Employee AS e1,Employee AS e2 WHERE e1.ManagerId = e2.Id AND e1.Salary > e2.Salary;


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