LeetCode MySQL 185. 部门工资前三高的所有员工(dense_rank)
生活随笔
收集整理的這篇文章主要介紹了
LeetCode MySQL 185. 部门工资前三高的所有员工(dense_rank)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
Employee 表包含所有員工信息,每個員工有其對應的工號 Id,姓名 Name,工資 Salary 和部門編號 DepartmentId 。
+----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 85000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | | 7 | Will | 70000 | 1 | +----+-------+--------+--------------+Department 表包含公司所有部門的信息。
+----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+編寫一個 SQL 查詢,找出每個部門獲得前三高工資的所有員工。
例如,根據上述給定的表,查詢結果應返回:
+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Randy | 85000 | | IT | Joe | 85000 | | IT | Will | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+ 解釋:IT 部門中, Max 獲得了最高的工資, Randy 和 Joe 都拿到了第二高的工資, Will 的工資排第三。銷售部門(Sales)只有兩名員工, Henry 的工資最高,Sam 的工資排第二。來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/department-top-three-salaries
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題
# Write your MySQL query statement below select Department, Employee, Salary from (select d.Name Department, e.Name Employee, Salary,dense_rank() over(partition by d.Name order by Salary desc) rnkfrom Employee e left join Department don e.DepartmentId = d.Id ) t where rnk <= 3 and Department is not nullor
# Write your MySQL query statement below select Department, Employee, Salary from (select d.Name Department, e.Name Employee, Salary,dense_rank() over(partition by d.Name order by Salary desc) rnkfrom Employee e right join Department don e.DepartmentId = d.Id ) t where rnk <= 3 and Employee is not nullor 使用內連接,自動去除 null 的行
select Department, Employee, Salary from (select d.Name Department, e.Name Employee, Salary,dense_rank() over(partition by d.Name order by Salary desc) rnkfrom Employee e join Department don e.DepartmentId = d.Id ) t where rnk <= 3我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的LeetCode MySQL 185. 部门工资前三高的所有员工(dense_rank)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 489. 扫地机器人(
- 下一篇: spacy 报错 gold.pyx in