-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetCode 184 Department Highest Salary.sql
60 lines (47 loc) · 1.86 KB
/
LeetCode 184 Department Highest Salary.sql
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
set search_path to data_sci
create index idx_id on company_departments(id)
-- Three different ways
-- All three have about the same performance
-- Find max salary and name in each department
-- Using a CTE, find max salary in each dept using the group by
-- Then get the employee name by joining with employees table
with max_dept_sal as
(SELECT max(e.salary) max_sal, d.id, d.department_name
from employees e
inner join company_departments d on e.department_id = d.id
group by d.id
)
SELECT e.last_name, e.salary, maxs.department_name, maxs.id
from employees e
inner join max_dept_sal maxs on e.department_id = maxs.id
where e.salary = maxs.max_sal
"greene" 149835 "other" 22
-- Just a cheeck for one val
SELECT e.last_name, e.salary, d.department_name, d.id
from employees e
inner join company_departments d on e.department_id = d.id
where d.id = 22
order by e.salary desc
-- Same thing but using a subquery to find max salary in each dept using the group by
-- Then get the employee name by joining with employees table
SELECT e.last_name, e.salary, maxs.department_name, maxs.id
from employees e
inner join
(SELECT max(e.salary) max_sal, d.id, d.department_name
from employees e
inner join company_departments d on e.department_id = d.id
group by d.id
) as maxs
on e.department_id = maxs.id
where e.salary = maxs.max_sal
-- This version is based on a solution from LeetCode
-- https://leetcode.com/problems/department-highest-salary/discuss/2436319/MYSQL-solution-faster-than-99-of-submissions
-- Use a subquery that ranks salary in each department. Use a Where to select top rank.
Select e.last_name, e.salary, d.department_name
from company_departments d
inner join
(SELECT e2.department_id, e2.last_name, e2.salary,
dense_rank() over(Partition by e2.department_id order by e2.salary desc) as sal_rank
from employees e2) as e
on e.department_id = d.id
where sal_rank=1