部门工资前三高的所有员工

 编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工,来源LeetCode,困难级别。
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    |
+----+----------+

根据上述给定的表,查询结果应返回:
IT 部门中,Max 获得了最高的工资,RANDy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | RANDy    | 85000  |
| IT         | Joe      | 85000  |
| IT         | Will     | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+
解题思路

1.两个别名表去重后进行join,以E1.Salary1的值为基准,设为A,求出E2.Salary2中比A大的记录的数量,这个数量就是”Employees表中去重后大于E1.Salary1的记录 “的个数
再根据DepartmentId和E1.Salary1分组和过滤,当数量小于等于3时, E1.Salary1即为各个DepartmentId中收入前三的的值

1
2
3
4
5
6
7
SELECT E1.Salary1 AS 'Salary', E1.DepartmentId1 AS 'Department', COUNT(*) AS 'COUNT'
FROM (SELECT DISTINCT Salary AS 'Salary1', DepartmentId AS 'DepartmentId1' FROM Employee) E1,
(SELECT DISTINCT Salary AS 'Salary2', DepartmentId AS 'DepartmentId2' FROM Employee) E2
WHERE E1.Salary1 <= E2.Salary2
AND E1.DepartmentId1 = E2.DepartmentId2
GROUP BY E1.Salary1, E1.DepartmentId1
having COUNT <= 3;

2.对收入前三的值进行min求出最小值,即各个部门收入前三中最小的那个值

1
2
3
4
5
6
7
8
9
10
SELECT min(Salary) AS 'Salary', Department
FROM (
SELECT E1.Salary1 AS 'Salary', E1.DepartmentId1 AS 'Department', COUNT(*) AS 'COUNT'
FROM (SELECT DISTINCT Salary AS 'Salary1', DepartmentId AS 'DepartmentId1' FROM Employee) E1,
(SELECT DISTINCT Salary AS 'Salary2', DepartmentId AS 'DepartmentId2' FROM Employee) E2
WHERE E1.Salary1 <= E2.Salary2
AND E1.DepartmentId1 = E2.DepartmentId2
GROUP BY E1.Salary1, E1.DepartmentId1
HAVING COUNT <= 3) tmp
GROUP BY Department;

3.求出各个部门收入前三中最小的那个值(tmp.Salary)之后,与employee表和department表进行关联,查询employee表中收入比tmp.Salary大的记录,即处于各个部门收入前三中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SELECT DISTINCT department.Name AS 'Department', employee.Name AS 'Employee', employee.Salary
FROM
(
SELECT MIN(Salary) AS 'Salary', Department
FROM (
SELECT E1.Salary1 AS 'Salary', E1.DepartmentId1 AS 'Department', COUNT(*) AS 'COUNT'
FROM (SELECT DISTINCT Salary AS 'Salary1', DepartmentId AS 'DepartmentId1' FROM Employee) E1,
(SELECT DISTINCT Salary AS 'Salary2', DepartmentId AS 'DepartmentId2' FROM Employee) E2
WHERE E1.Salary1 <= E2.Salary2
AND E1.DepartmentId1 = E2.DepartmentId2
GROUP BY E1.Salary1, E1.DepartmentId1
having COUNT <= 3) tmp
GROUP BY Department
) tmp,
employee,
department
WHERE employee.DepartmentId = department.Id
AND employee.DepartmentId = tmp.Department
AND tmp.Salary <= employee.Salary
ORDER BY Department, Salary DESC;
运行结果

战胜了96.33%的提交记录

运行结果

LeetCode官方答案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT
d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary
FROM
Employee e1
JOIN
Department d ON e1.DepartmentId = d.Id
WHERE
3 > (SELECT
COUNT(DISTINCT e2.Salary)
FROM
Employee e2
WHERE
e2.Salary > e1.Salary
AND e1.DepartmentId = e2.DepartmentId
)

官方解采用关联子查询,将E1每一行记录传入子查询中,子查询中求出以e1.Salary为基准,在E1.DepartmentId中,E2.Salary大于e1.Salary的数量,即e1.Salary在Employee表(DepartmentId约束下)中的排名,主查询的where 3 > “子查询结果”,过滤了子查询的结果,要求e1.Salary在子查询中得到的结果要小于3,即e1.Salary在Employee表(DepartmentId约束下)中的排名小于3