-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path11-department-top-3-salaries.sql
50 lines (34 loc) · 1.33 KB
/
11-department-top-3-salaries.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
-- A company's executives are interested in seeing who earns the most money in each of the company's departments. A high earner in a department is an employee who has a salary in the top three unique salaries for that department.
-- Write an SQL query to find the employees who are high earners in each of the departments.
WITH ranked_salaries as (
SELECT e.Name AS Employee,
e.Salary,
d.Name AS Department,
DENSE_RANK() OVER(PARTITION BY DepartmentId ORDER BY Salary desc) as rank
FROM Employee e JOIN Department d ON e.DepartmentId = d.Id
)
SELECT Department, Employee, Salary
FROM ranked_salaries
where rank < 4
-- another approach: close to the solution, j
SELECT d.Name AS Department, e.Name AS Employee, e.Salary
FROM Employee e
INNER JOIN Department d ON e.DepartmentId = d.Id
WHERE e.Salary IN (
SELECT DISTINCT Salary
FROM Employee e2
WHERE e2.DepartmentId = e.DepartmentId
ORDER BY Salary DESC
LIMIT 3
);
-- my approach:
WITH ranked_salaries as (
SELECT *, DENSE_RANK() OVER(PARTITION BY DepartmentId ORDER BY Salary) as rank
FROM Employee e JOIN Department d USING(Id)
)
SELECT Name as Department, Name as Employee, Salary
FROM ranked_salaries
where rank < 4
-- remarks:
-- 1. messed up JOIN by using different common column;
-- 2. forgot "DESC" in order by.