Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit fdeec3c

Browse files
authored
department-top-three-salaries
1 parent 1173045 commit fdeec3c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Time: O(n^2)
22
# Space: O(n)
33

4-
SELECT D.Name AS Department, E.Name AS Employee, E.Salary AS Salary
5-
FROM Employee E INNER JOIN Department D ON E.DepartmentId = D.Id
6-
WHERE (SELECT COUNT(DISTINCT(Salary)) FROM Employee
7-
WHERE DepartmentId = E.DepartmentId AND Salary > E.Salary) < 3
8-
ORDER by E.DepartmentId, E.Salary DESC;
4+
SELECT dp.name AS Department,ab.name AS Employee, ab.Salary
5+
FROM
6+
(SELECT *,
7+
dense_rank() OVER(PARTITION BY departmentID ORDER BY salary DESC) AS sal_rank
8+
FROM Employee) ab
9+
LEFT JOIN Department dp ON ab.departmentId = dp.id
10+
WHERE ab.sal_rank <=3
911

0 commit comments

Comments
 (0)