主頁 > 知識庫 > SQL實現(xiàn)LeetCode(184.系里最高薪水)

SQL實現(xiàn)LeetCode(184.系里最高薪水)

熱門標簽:智能外呼系統(tǒng)復位 拉卡拉外呼系統(tǒng) 外東北地圖標注 云南電商智能外呼系統(tǒng)價格 臨清電話機器人 大眾點評星級酒店地圖標注 話務(wù)外呼系統(tǒng)怎么樣 高清地圖標注道路 400電話可以辦理嗎

[LeetCode] 184.Department Highest Salary 系里最高薪水

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| Sales      | Henry    | 80000  |
+------------+----------+--------+

這道題讓給了我們兩張表,Employee表和Department表,讓我們找系里面薪水最高的人的,實際上這題是Second Highest SalaryCombine Two Tables的結(jié)合題,我們既需要聯(lián)合兩表,又要找到最高薪水,那么我們首先讓兩個表內(nèi)交起來,然后將結(jié)果表需要的列都標明,然后就是要找最高的薪水,我們用Max關(guān)鍵字來實現(xiàn),參見代碼如下:

解法一:

SELECT d.Name AS Department, e1.Name AS Employee, e1.Salary FROM Employee e1
JOIN Department d ON e1.DepartmentId = d.Id WHERE Salary IN 
(SELECT MAX(Salary) FROM Employee e2 WHERE e1.DepartmentId = e2.DepartmentId);

我們也可以不用Join關(guān)鍵字,直接用Where將兩表連起來,然后找最高薪水的方法和上面相同:

解法二:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department d
WHERE e.DepartmentId = d.Id AND e.Salary = (SELECT MAX(Salary) FROM Employee e2 WHERE e2.DepartmentId = d.Id);

下面這種方法沒用用到Max關(guān)鍵字,而是用了>=符號,實現(xiàn)的效果跟Max關(guān)鍵字相同,參見代碼如下:

解法三:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department d
WHERE e.DepartmentId = d.Id AND e.Salary >= ALL (SELECT Salary FROM Employee e2 WHERE e2.DepartmentId = d.Id);

類似題目:

Second Highest Salary

Combine Two Tables

到此這篇關(guān)于SQL實現(xiàn)LeetCode(184.系里最高薪水)的文章就介紹到這了,更多相關(guān)SQL實現(xiàn)系里最高薪水內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • SQL實現(xiàn)LeetCode(196.刪除重復郵箱)
  • SQL實現(xiàn)LeetCode(185.系里前三高薪水)
  • SQL實現(xiàn)LeetCode(183.從未下單訂購的顧客)
  • SQL實現(xiàn)LeetCode(182.重復的郵箱)
  • SQL實現(xiàn)LeetCode(181.員工掙得比經(jīng)理多)
  • SQL實現(xiàn)LeetCode(180.連續(xù)的數(shù)字)
  • C++實現(xiàn)LeetCode(179.最大組合數(shù))
  • SQL實現(xiàn)LeetCode(197.上升溫度)

標簽:福州 山西 定西 阿里 無錫 三明 揚州 溫州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《SQL實現(xiàn)LeetCode(184.系里最高薪水)》,本文關(guān)鍵詞  SQL,實現(xiàn),LeetCode,184.,系里,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQL實現(xiàn)LeetCode(184.系里最高薪水)》相關(guān)的同類信息!
  • 本頁收集關(guān)于SQL實現(xiàn)LeetCode(184.系里最高薪水)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章