-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path185. Department Top Three Salaries.sql
More file actions
56 lines (46 loc) · 1.65 KB
/
185. Department Top Three Salaries.sql
File metadata and controls
56 lines (46 loc) · 1.65 KB
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
56
/*
Question 185. Department Top Three Salaries
Link: https://leetcode.com/problems/department-top-three-salaries/description/
Table: Employee
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| id | int |
| name | varchar |
| salary | int |
| departmentId | int |
+--------------+---------+
id is the primary key (column with unique values) for this table.
departmentId is a foreign key (reference column) of the ID from the Department table.
Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.
Table: Department
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table indicates the ID of a department and its name.
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 a solution to find the employees who are high earners in each of the departments.
Return the result table in any order.
*/
SELECT
d.name AS Department,
e.name AS Employee,
e.salary
FROM Employee AS e
LEFT JOIN
Department AS d
ON e.departmentId = d.id
WHERE (e.departmentId, e.salary) IN (
SELECT
e2.departmentId,
e2.salary
FROM Employee AS e2
WHERE e2.departmentId = d.id
GROUP BY e2.departmentId, e2.salary
ORDER BY e2.salary DESC
LIMIT 3
)