SQL实现LeetCode(185.系里前三高薪水)
作者:Grandyang 时间:2024-01-18 19:12:22
[LeetCode] 185.Department Top Three Salaries 系里前三高薪水
The Employee table holds all employees. Every employee has an Id, 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 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
+----+-------+--------+--------------+
The Department table holds all departments of the company.
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
这道题是之前那道Department Highest Salary的拓展,难度标记为Hard,还是蛮有难度的一道题,综合了前面很多题的知识点,首先看使用Select Count(Distinct)的方法,我们内交Employee和Department两张表,然后我们找出比当前薪水高的最多只能有两个,那么前三高的都能被取出来了,参见代码如下:
解法一:
SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e
JOIN Department d on e.DepartmentId = d.Id
WHERE (SELECT COUNT(DISTINCT Salary) FROM Employee WHERE Salary > e.Salary
AND DepartmentId = d.Id) < 3 ORDER BY d.Name, e.Salary DESC;
下面这种方法将上面方法中的<3换成了IN (0, 1, 2),是一样的效果:
解法二:
SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department d
WHERE (SELECT COUNT(DISTINCT Salary) FROM Employee WHERE Salary > e.Salary
AND DepartmentId = d.Id) IN (0, 1, 2) AND e.DepartmentId = d.Id ORDER BY d.Name, e.Salary DESC;
或者我们也可以使用Group by Having Count(Distinct ..) 关键字来做:
解法三:
SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM
(SELECT e1.Name, e1.Salary, e1.DepartmentId FROM Employee e1 JOIN Employee e2
ON e1.DepartmentId = e2.DepartmentId AND e1.Salary <= e2.Salary GROUP BY e1.Id
HAVING COUNT(DISTINCT e2.Salary) <= 3) e JOIN Department d ON e.DepartmentId = d.Id
ORDER BY d.Name, e.Salary DESC;
下面这种方法略微复杂一些,用到了变量,跟Consecutive Numbers中的解法三使用的方法一样,目的是为了给每个人都按照薪水的高低增加一个rank,最后返回rank值小于等于3的项即可,参见代码如下:
解法四:
SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM
(SELECT Name, Salary, DepartmentId,
@rank := IF(@pre_d = DepartmentId, @rank + (@pre_s <> Salary), 1) AS rank,
@pre_d := DepartmentId, @pre_s := Salary
FROM Employee, (SELECT @pre_d := -1, @pre_s := -1, @rank := 1) AS init
ORDER BY DepartmentId, Salary DESC) e JOIN Department d ON e.DepartmentId = d.Id
WHERE e.rank <= 3 ORDER BY d.Name, e.Salary DESC;
类似题目:
Department Highest Salary
Second Highest Salary
Combine Two Tables
参考资料:
https://leetcode.com/discuss/23002/my-tidy-solution
https://leetcode.com/discuss/91087/yet-another-solution-using-having-count-distinct
https://leetcode.com/discuss/69880/two-solutions-1-count-join-2-three-variables-join
来源:https://www.cnblogs.com/grandyang/p/5367670.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 在服务器上调用数据库特别慢的解决过程
![](https://img.aspxhome.com/file/2023/6/121876_0s.png)
Linux下mysql 5.6.17安装图文教程详细版
![](https://img.aspxhome.com/file/2023/8/118438_0s.png)
Mysql的基础使用之MariaDB安装方法详解
![](https://img.aspxhome.com/file/2023/2/99352_0s.jpg)
在Python反编译中批量pyc转 py的实现代码
![](https://img.aspxhome.com/file/2023/2/61732_0s.png)
详解vue过度效果与动画transition使用示例
![](https://img.aspxhome.com/file/2023/6/139776_0s.gif)
利用Python实现图书超期提醒
![](https://img.aspxhome.com/file/2023/8/131328_0s.png)
js RuntimeObject() 获取ie里面自定义函数或者属性的集合
go语言中的return语句
MySQL数据库基本SQL语句教程之高级操作
![](https://img.aspxhome.com/file/2023/8/76468_0s.png)
ASP.NET2.0数据库入门之SqlDataSource
Python读写Excel表格的方法
![](https://img.aspxhome.com/file/2023/6/118556_0s.jpg)
php5.3 不支持 session_register() 此函数已启用的解决方法
使用python实现画AR模型时序图
![](https://img.aspxhome.com/file/2023/8/66278_0s.jpg)
Python实现的各种常见分布算法示例
![](https://img.aspxhome.com/file/2023/1/76061_0s.png)
python中redis查看剩余过期时间及用正则通配符批量删除key的方法
vue使用v-for实现hover点击效果
![](https://img.aspxhome.com/file/2023/1/130041_0s.png)
Pandas替换及部分替换(replace)实现流程详解
![](https://img.aspxhome.com/file/2023/9/125179_0s.png)
ASP中ServerVariables集合用法详解
Python matplotlib绘制散点图配置(万能模板案例)
![](https://img.aspxhome.com/file/2023/9/61989_0s.png)