SQL實現(xiàn)LeetCode(181.員工掙得比經(jīng)理多)
[LeetCode] 181.Employees Earning More Than Their Managers 員工掙得比經(jīng)理多
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表,里面有員工的薪水信息和其經(jīng)理的信息,經(jīng)理也屬于員工,其經(jīng)理Id為空,讓我們找出薪水比其經(jīng)理高的員工,那么就是一個很簡單的比較問題了,我們可以生成兩個實例對象進行內(nèi)交通過ManagerId和Id,然后限制條件是一個Salary大于另一個即可:
解法一:
SELECT e1.Name FROM Employee e1 JOIN Employee e2 ON e1.ManagerId = e2.Id WHERE e1.Salary > e2.Salary;
我們也可以不用Join,直接把條件都寫到where里也行:
解法二:
SELECT e1.Name FROM Employee e1, Employee e2 WHERE e1.ManagerId = e2.Id AND e1.Salary > e2.Salary;
參考資料:
https://leetcode.com/discuss/88189/two-straightforward-way-using-where-and-join
到此這篇關(guān)于SQL實現(xiàn)LeetCode(181.員工掙得比經(jīng)理多)的文章就介紹到這了,更多相關(guān)SQL實現(xiàn)員工掙得比經(jīng)理多內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql 5.7安裝 MySQL 服務(wù)無法啟動但是服務(wù)沒有報告任何錯誤
這篇文章主要介紹了mysql 5.7安裝 MySQL 服務(wù)無法啟動但是服務(wù)沒有報告任何錯誤的相關(guān)資料,需要的朋友可以參考下2017-04-04
MySQL中SQL連接操作左連接查詢(LEFT?JOIN)示例詳解
這篇文章主要給大家介紹了關(guān)于MySQL中SQL連接操作左連接查詢(LEFT?JOIN)的相關(guān)資料,左連接(LEFT?JOIN)是SQL中用于連接兩個或多個表的一種操作,它返回左表的所有行,并根據(jù)連接條件從右表中匹配行,需要的朋友可以參考下2024-12-12

