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

标签:SQL,系里前三高薪水,LeetCode
0
投稿

猜你喜欢

  • 数字格式化转换

    2010-08-03 12:22:00
  • python实现AI聊天机器人详解流程

    2022-12-11 23:57:37
  • tkinter自定义下拉多选框问题

    2022-09-27 06:40:00
  • Python urllib 入门使用详细教程

    2023-03-02 10:48:35
  • Python爬虫实现的根据分类爬取豆瓣电影信息功能示例

    2022-10-14 04:27:51
  • sqlserver 2000中创建用户的图文方法

    2012-03-26 18:26:39
  • pip install命令安装扩展库整理

    2021-10-13 21:49:14
  • Python3之简单搭建自带服务器的实例讲解

    2022-03-05 20:50:33
  • WebSocket部署服务器但外网无法连接的解决方法

    2023-04-14 05:26:20
  • python tkinter 做个简单的计算器的方法

    2023-12-20 18:18:27
  • 将tensorflow.Variable中的某些元素取出组成一个新的矩阵示例

    2022-01-17 23:49:50
  • 听歌识曲--用python实现一个音乐检索器的功能

    2021-11-01 00:46:03
  • Python常用数据类型之间的转换总结

    2023-06-21 10:06:21
  • Python的Twisted框架上手前所必须了解的异步编程思想

    2021-05-02 14:21:44
  • sqlserver中操作主从关系表的sql语句

    2024-01-22 09:13:14
  • Python中的Selenium异常处理

    2021-08-28 04:15:23
  • Pygame鼠标进行图片的移动与缩放案例详解

    2023-08-12 15:18:58
  • python-django中的APPEND_SLASH实现方法

    2023-04-05 07:17:57
  • 如何真正的了解python装饰器

    2023-03-16 09:08:23
  • python寻找含有关键字文件和删除文件夹方式

    2021-08-22 05:46:32
  • asp之家 网络编程 m.aspxhome.com