MySQL部门工资前三高的所有员工
Posted willem_chen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL部门工资前三高的所有员工相关的知识,希望对你有一定的参考价值。
SQL架构
Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int);
Create table If Not Exists Department (Id int, Name varchar(255));
insert into Employee (Id, Name, Salary, DepartmentId) values ('1', 'Joe', '85000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('2', 'Henry', '80000', '2');
insert into Employee (Id, Name, Salary, DepartmentId) values ('3', 'Sam', '60000', '2');
insert into Employee (Id, Name, Salary, DepartmentId) values ('4', 'Max', '90000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('5', 'Janet', '69000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('6', 'Randy', '85000', '1');
insert into Employee (Id, Name, Salary, DepartmentId) values ('7', 'Will', '70000', '1');
insert into Department (Id, Name) values ('1', 'IT');
insert into Department (Id, Name) values ('2', 'Sales');
题目描述
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 的工资排第二。
题解
方法:使用 JOIN 和子查询
算法
公司里前 3 高的薪水意味着有不超过 3 个工资比这些值大。
select e1.Name as 'Employee', e1.Salary
from Employee e1
where 3 >
(
select count(distinct e2.Salary)
from Employee e2
where e2.Salary > e1.Salary
);
在这个代码里,我们统计了有多少人的工资比 e1.Salary 高,所以样例的输出应该如下所示。
+----------+--------+
| Employee | Salary |
+----------+--------+
| Joe | 85000 |
| Henry | 80000 |
| Max | 90000 |
| Randy | 85000 |
+----------+--------+
4 rows in set (0.00 sec)
然后,我们需要把表 Employee 和表 Department 连接来获得部门信息。
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
);
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Joe | 85000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Will | 70000 |
+------------+----------+--------+
6 rows in set (0.00 sec)
SELECT
Department.NAME AS Department,
e1.NAME AS Employee,
e1.Salary AS Salary
FROM
Employee AS e1,Department
WHERE
e1.DepartmentId = Department.Id
AND 3 > (SELECT count( DISTINCT e2.Salary )
FROM Employee AS e2
WHERE e1.Salary < e2.Salary AND e1.DepartmentId = e2.DepartmentId )
ORDER BY Department.NAME,Salary DESC;
解题思路
select
d.name as Department, e1.name as employee, e1.salary as salary
from
Department d
join Employee e1 on d.id = e1.departmentid
join Employee e2 on e1.departmentid = e2.departmentid and e1.salary<=e2.salary
group by
d.name, e1.name
having
count(distinct e2.salary)<=3
order by
d.name, e1.salary desc;
+------------+----------+--------+
| Department | employee | salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Joe | 85000 |
| IT | Randy | 85000 |
| IT | Will | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
6 rows in set (0.00 sec)
SELECT
d.NAME department, t.NAME employee, salary
FROM
(
SELECT *,
@r := IF(@pD = departmentid, IF(@pS = salary, @r, @r + 1 ), 1 ) AS 'rank',
@pD := departmentid,
@pS := salary
FROM
Employee, ( SELECT @pS := NULL, @pD := NULL, @r := 0 ) init
ORDER BY
departmentid, salary DESC
) t
JOIN Department d ON t.departmentid = d.id
WHERE
t.rank <=3;
以上是关于MySQL部门工资前三高的所有员工的主要内容,如果未能解决你的问题,请参考以下文章
⭐️ LeetCode解题系列 ⭐️ 185. 部门工资前三高的所有员工(Oracle dense_rank函数)