資料庫: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 |
+------------+----------+--------+
非常常見的資料庫查詢問題,以部門為單位求最高工資。
上傳這道題的目的是為提高對查詢語句的理解:
先公佈解法:
SELECT d.Name AS Department, e1.Name AS對網上這種解法一開始並不能看懂,因為固定思維on後面有一個=連線的條件,總是會讓我想到結果集只有一行資料。然而並不是如此,首先,select語句可以看成是迴圈語句,遍歷表中的每一行資料並篩選出符合條件的,如果這樣理解,那麼=號後面子查詢的結果會隨遍歷而變化,當指標指到並列後表中的第一條資料時,子查詢結果為90000,(因為此時DepartmentId為1).因此第一條資料不符合條件。以此類推能夠得到正確結果。 我的第一次解法:Employee, e1.Salary FROM Employee e1 JOIN Department d ON e1.DepartmentId = d.Id WHERE Salary = (SELECT MAX(Salary) FROM Employee e2 WHERE e1.DepartmentId = e2.DepartmentId);
SELECT d.Name AS Department, e1.Name AS Employee, MAX(e1.Salary) Salary FROM Employee e1 JOIN Department d ON e1.DepartmentId = d.Id GROUP BY Department;看似沒有什麼問題,按部門為單位求最大值,因為之前按部門為單位求和時就是這樣寫的。兩者的區別為:按部門求和問題最終資料是所屬於部門的,而最大值是所屬於部門中的某人的,這樣執行的結果就是工資和員工不匹配,因為MAX只負責求某列最大值,這樣執行的結果是最大值正確無誤而對應的員工則是部門中編號靠前的第一個員工,因為並沒有條件限制員工。剩下的被捨棄,因為MAX只有一行資料。 總結: select語句的正確理解和group by 的適當運用。
相關推薦
資料庫:184. Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a
184. Department Highest Salary (medium)
https pro table select company leet have fin ems Source: https://leetcode.com/problems/department-highest-salary/#/descriptionDescription
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. +----+---
【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. +----+----
LeetCode-Algorithms #009 Palindrome Number, Database #184 Department Highest Salary
LeetCode-Algorithms #009 Palindrome Number 判斷一個整數是否是迴文數. 進階版: 不將原數轉化為字串的前提下判斷一個數是否是迴文數 先用字串做一次: 1 class Solution { 2 public boolean isPalindrome(in
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. +----+---
[SQL]LeetCode184. 部門工資最高的員工 | 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. +----+-------+----
[LeetCode] 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. +----+-------+--------+---
Leetcode: 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. +—-+
176. Second Highest Salary(Easy)
tco targe query rip con -s second count rom Source of the question Write a SQL query to get the second highest salary from the Employee t
177. Nth Highest Salary (Medium)
desc nth logs esc employee example leetcode turn begin Source: https://leetcode.com/problems/nth-highest-salary/#/descriptionDescription:
176. Second Highest Salary
max color class statement select mysql ble clas cond Write a SQL query to get the second highest salary from the Employee table. +----+-
找第二大的數SQL-Second Highest Salary
des rom tin .com begin get pre sql 最大 1: 找小於最大的最大的 select max(Salary) from Employee where Salary<(select MAX(Salary) from Employe
sql leetcode 176. Second Highest Salary
Write a SQL query to get the second highest salary from the Employeetable. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2
LeetCode-第二高的薪水(second-highest-salary)
第二高的薪水 難度 簡單 更多LeetCode答案歡迎大家關注Github: https://github.com/lxyer/LeetCodeAnswer 編寫一個 SQL 查詢,獲取 Employee 表中第二高的薪水(Salary) 。 +----+---
圖資料庫:AgensGraph
文章目錄 AgensGraph簡介 官網及下載 安裝AgensGraph 上傳並解壓 新增agens使用者 配置.bashrc 初始化並啟動
MFC ado資料庫:所有表名稱
class CAdoDatabase { public: CAdoDatabase(); virtual ~CAdoDatabase(); BOOL OpenMDB(CString strPath); BOOL CreateTable(CString
資料庫:檢視和索引
目錄 一、檢視 1.什麼是檢視? 2.為什麼使用檢視? 3.如何使用檢視? 二、索引 1.什麼是索引? 2.為什麼使用索引? 2.如何使用索引?(建立、刪除) 3.適用場景有哪些? 4.注意事項有哪些? 一、檢視 1.什麼是檢視? 檢視是一
資料庫:儲存程式
目錄 一、什麼是儲存程式? 二、為什麼使用儲存程式? 三、儲存程式分類 四、儲存過程 1.儲存過程引數的3中模式 2.流程控制語句 3.示例程式碼 五、儲存函式 六、觸發器 一、什麼是儲存程式? 儲存程式指的是一組儲存和執行在資料庫伺服器端的程式。
資料庫:事務
目錄 一、儲存引擎 1.什麼是儲存引擎? 2.常見的儲存引擎有哪些? 3.如何檢視和設定儲存引擎? 二、mysql字符集編碼 1.檢視字符集編碼 2.修改字符集編碼 三、事務 1.什麼是事務? 2.開啟和提交事務? 3.事務的ACID特性(面試經常問)