Original question: https://leetcode-cn.com/problems/department-top-three-salaries/
I made the problem-solving record for the database problem of leetcode:
solution directory https://blog.csdn.net/weixin_42845682/ article / details / 105196004 This
question is very interesting, it is recommended to refer to it: 184. The highest paid employee in the department
https://blog.csdn.net/weixin_42845682/article/details/105437193
Title description:
The Employee table contains all employee information, and each employee has its corresponding job ID, name, salary Salary, and department ID 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 |
+----+-------+--------+--------------+
The Department table contains information about all departments of the company.
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
Write a SQL query to find out all employees who received the top three high salaries in each department. For example, according to the table given above, the query result should return:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 85000 |
| IT | Will | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
Explanation:
In the IT department, Max received the highest salary, Randy and Joe both received the second highest salary, and Will's salary ranked third. The sales department (Sales) has only two employees. Henry has the highest salary and Sam has the second highest salary.
answer:
The first answer
According to the article described and referenced in the title, copy the SQL using dense_rank () directly, and replace the last = 1 with <= 3.
select
dname department,
ename employee,
salary
from(
select
d.name dname,
e.name ename,
e.salary,
dense_rank() over(partition by d.name order by salary desc) row_num
from employee e
join department d on e.departmentId=d.id
) tmp
where row_num <=3
Why is it so slow?
The second answer
What if the use of mysql does not support dense_rank ()? (I heard that mysql8.0 supports over ().)
select
d.name Department,
e.name Employee,
e.salary Salary
from employee e
join department d on e.departmentid = d.id
where 3>
(
select
count(distinct(e2.salary))
from employee e2
where e.departmentid=e2.departmentid
and e2.salary > e.salary
)
At the beginning of writing, the filter condition behind where was written as 3> = ..., and as a result, the top four high wages of the department appeared, and it was a bit unthinkable at 1:30. Later figured it out: suppose the first three highs of the department are 30000, 20000, 10000, then there are only 0 higher than 30,000, then only 1 higher than 20000, then only 2 higher than 10000.
select
d.name Department,
e.name Employee,
e.salary Salary
from employee e
join department d on e.departmentid = d.id
where 3>=
(
select
count(distinct(e2.salary))
from employee e2
where e.departmentid=e2.departmentid
and e2.salary >= e.salary
)
Although these two sql are basically the same, but I still like this way of writing. . .
ps: I hope I never have to write this kind of sql.