Skip to content

Commit 29abfe2

Browse files
committed
Upload 136
1 parent dbb2005 commit 29abfe2

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ The problems and both submitted and site solutions are documented in individual
168168
| 133 | [Restaurant Growth](https://leetcode.com/problems/restaurant-growth/description/) | [Solution](solutions/133_restaurant_growth.md) | LeetCode | Medium | |
169169
| 134 | [Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/description/) | [Solution](solutions/134_friend_requests_ii_who_has_the_most_friends.md) | LeetCode | Medium | `UNION ALL` |
170170
| 135 | [Investments in 2016](https://leetcode.com/problems/investments-in-2016/description/) | [Solution](solutions/135_investments_in_2016.md) | LeetCode | Medium | |
171+
| 136 | [Department Top Three Salaries](https://leetcode.com/problems/department-top-three-salaries/description/) | [Solution](solutions/136_department_top_three_salaries.md) | LeetCode | Hard | |
171172
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
172173

173174
## Author(s)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# SQL Everyday \#136
2+
3+
## Department Top Three Salaries
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Hard
7+
8+
## Problem
9+
10+
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.
11+
12+
Write a solution to find the employees who are *high earners* in each of the departments.
13+
14+
Return the result table *in any order*. [[Full Description](https://leetcode.com/problems/department-top-three-salaries/description/)]
15+
16+
## Submitted Solution
17+
18+
```sql
19+
-- Submitted Solution
20+
WITH cte AS (
21+
SELECT
22+
d.name AS Department
23+
,e.name AS Employee
24+
,e.salary AS Salary
25+
,DENSE_RANK() OVER (PARTITION BY d.name ORDER BY e.salary DESC) AS rank
26+
FROM Employee AS e
27+
JOIN Department AS d ON e.departmentId = d.id
28+
)
29+
SELECT
30+
Department
31+
,Employee
32+
,Salary
33+
FROM cte
34+
WHERE rank <= 3
35+
ORDER BY Department ASC, Employee ASC, Salary DESC
36+
;
37+
```
38+
39+
## Site Solution
40+
41+
```sql
42+
-- LeetCode Solution
43+
SELECT d.name AS 'Department',
44+
e1.name AS 'Employee',
45+
e1.salary AS 'Salary'
46+
FROM Employee e1
47+
JOIN Department d
48+
ON e1.departmentId = d.id
49+
WHERE
50+
3 > (SELECT COUNT(DISTINCT e2.salary)
51+
FROM Employee e2
52+
WHERE e2.salary > e1.salary AND e1.departmentId = e2.departmentId);
53+
```
54+
55+
## Notes
56+
57+
TBD
58+
59+
## NB
60+
61+
62+
63+
Go to [Index](../?tab=readme-ov-file#index)\
64+
Go to [Overview](../?tab=readme-ov-file)
65+

0 commit comments

Comments
 (0)