SQL實(shí)現(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 |
+----------+
這道題給我們了一個(gè)Employee表,里面有員工的薪水信息和其經(jīng)理的信息,經(jīng)理也屬于員工,其經(jīng)理Id為空,讓我們找出薪水比其經(jīng)理高的員工,那么就是一個(gè)很簡(jiǎn)單的比較問(wèn)題了,我們可以生成兩個(gè)實(shí)例對(duì)象進(jìn)行內(nèi)交通過(guò)ManagerId和Id,然后限制條件是一個(gè)Salary大于另一個(gè)即可:
解法一:
SELECT e1.Name FROM Employee e1 JOIN Employee e2 ON e1.ManagerId = e2.Id WHERE e1.Salary > e2.Salary;
我們也可以不用Join,直接把條件都寫(xiě)到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實(shí)現(xiàn)LeetCode(181.員工掙得比經(jīng)理多)的文章就介紹到這了,更多相關(guān)SQL實(shí)現(xiàn)員工掙得比經(jīng)理多內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SQL實(shí)現(xiàn)LeetCode(196.刪除重復(fù)郵箱)
- SQL實(shí)現(xiàn)LeetCode(185.系里前三高薪水)
- SQL實(shí)現(xiàn)LeetCode(184.系里最高薪水)
- SQL實(shí)現(xiàn)LeetCode(183.從未下單訂購(gòu)的顧客)
- SQL實(shí)現(xiàn)LeetCode(182.重復(fù)的郵箱)
- SQL實(shí)現(xiàn)LeetCode(180.連續(xù)的數(shù)字)
- C++實(shí)現(xiàn)LeetCode(179.最大組合數(shù))
- SQL實(shí)現(xiàn)LeetCode(197.上升溫度)
相關(guān)文章
mysql 5.7.15 安裝配置方法圖文教程(windows)
這篇文章主要為大家詳細(xì)介紹了mysql 5.7.15 安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
MySQL分割字符串一行轉(zhuǎn)多行的實(shí)現(xiàn)方法
這篇文章主要介紹了MySQL分割字符串一行轉(zhuǎn)多行,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
利用JuiceFS使MySQL?備份驗(yàn)證性能提升?10?倍
這篇文章主要介紹了如何讓?MySQL?備份驗(yàn)證性能提升?10?倍,JuiceFS?非常適合用來(lái)做?MySQL?物理備份,通過(guò)不斷調(diào)整?XtraBackup?的參數(shù)和?JuiceFS?的掛載參數(shù),在一個(gè)小時(shí)內(nèi)將時(shí)間縮短到原先的?1/10,下文一起來(lái)看相關(guān)內(nèi)容的詳細(xì)介紹吧2022-03-03
mysql分區(qū)表的增刪改查的實(shí)現(xiàn)示例
增刪查改在數(shù)據(jù)庫(kù)中是很常見(jiàn)的操作,本文主要介紹了mysql分區(qū)表的增刪改查的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01

