Skip to content

Commit 2202090

Browse files
committed
task: #185
1 parent 5b748cf commit 2202090

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Have a good contributing!
6060
- [1045. Customers Who Bought All Products](./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql)
6161
- [1070. Product Sales Analysis III](./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql)
6262
- [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql)
63+
3. [Hard](./leetcode/hard/)
64+
- [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql)
6365

6466
## License
6567

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Question 185. Department Top Three Salaries
3+
Link: https://leetcode.com/problems/department-top-three-salaries/description/
4+
5+
Table: Employee
6+
7+
+--------------+---------+
8+
| Column Name | Type |
9+
+--------------+---------+
10+
| id | int |
11+
| name | varchar |
12+
| salary | int |
13+
| departmentId | int |
14+
+--------------+---------+
15+
id is the primary key (column with unique values) for this table.
16+
departmentId is a foreign key (reference column) of the ID from the Department table.
17+
Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.
18+
19+
20+
Table: Department
21+
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| id | int |
26+
| name | varchar |
27+
+-------------+---------+
28+
id is the primary key (column with unique values) for this table.
29+
Each row of this table indicates the ID of a department and its name.
30+
31+
32+
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.
33+
34+
Write a solution to find the employees who are high earners in each of the departments.
35+
36+
Return the result table in any order.
37+
*/
38+
39+
SELECT
40+
d.name AS Department,
41+
e.name AS Employee,
42+
e.salary
43+
FROM Employee AS e
44+
LEFT JOIN
45+
Department AS d
46+
ON e.departmentId = d.id
47+
WHERE (e.departmentId, e.salary) IN (
48+
SELECT
49+
e2.departmentId,
50+
e2.salary
51+
FROM Employee AS e2
52+
WHERE e2.departmentId = d.id
53+
GROUP BY e2.departmentId, e2.salary
54+
ORDER BY e2.salary DESC
55+
LIMIT 3
56+
)

0 commit comments

Comments
 (0)