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 |
+----------+
這種單表比較條件,一般都是表內進行join操作.
參照此思路,解題如下所示:
# Write your MySQL query statement below
SELECT
a.Name AS Employee
FROM Employee a, Employee b
WHERE
a.ManagerId = b.Id
AND a.Salary > b.Salary;
運行效率在可以接受的范圍,此外陳述句也較為清晰便于維護.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/122822.html
標籤:MySQL
上一篇:LeetCode——Consecutive Numbers
下一篇:深度學習入門(上)
