-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode184.sql
More file actions
31 lines (30 loc) · 915 Bytes
/
LeetCode184.sql
File metadata and controls
31 lines (30 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- 先查每个dpeartment中e.salary最大值,后寻找最大值对应的e.name
select d.name 'Department', e.name 'Employee', e.salary 'Salary'
from Employee e
left join Department d on d.id = e.departmentId
where
e.salary = (
select t.max_salary
from (
select departmentId, Max(salary) max_salary
from Employee
group by
departmentId
) t
where
e.departmentId = t.departmentId
);
-- 用窗口函数进行分组排序
select Department, Employee, Salary
from (
select
d.name Department, e.name Employee, e.salary Salary, dense_rank() over (
partition by
d.id
order by e.salary desc
) ranking
from Employee e
join Department d on e.departmentId = d.id
) t
where
ranking = 1;