前述
推荐阅读:通俗易懂的学会:SQL窗口函数
题目描述
leetcode题目 185. 部门工资前三高的所有员工
思路
- 先按照departmentId分组,再按照salary排序 ==>窗口函数dense_rank() over()
select
B.name as Department,
A.name as Employee,
A.salary as Salary
from (
select *,
dense_rank() over(partition by departmentId order by salary desc) as rk
from Employee
) A
join Department B
on B.id = A.departmentId
where A.rk <= 3