185. Department Top Three Salaries - Detailed Explanation
Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!
Problem Statement
Table Employee has the following schema:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | Id | int | | Name | varchar | | Salary | int | | DepartmentId | int | +---------------+---------+
Write a SQL query to report, for each department, the employees who earn one of the top three highest distinct salaries in that department. Return columns DepartmentId, Employee (the Name), and Salary, ordered by DepartmentId ascending and Salary descending. If a department has fewer than three distinct salaries, include all its employees.
Examples
Input
Employee table:
+----+------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Jim | 90000 | 1 |
| 3 | Henry| 80000 | 2 |
| 4 | Sam | 60000 | 2 |
| 5 | Max | 90000 | 1 |
+----+------+--------+--------------+
Output
+--------------+----------+--------+
| DepartmentId | Employee | Salary |
+--------------+----------+--------+
| 1 | Jim | 90000 |
| 1 | Max | 90000 |
| 1 | Joe | 70000 |
| 2 | Henry | 80000 |
| 2 | Sam | 60000 |
+--------------+----------+--------+
Hints
- You need to rank salaries within each department and pick those with rank ≤ 3.
- Use window functions (e.g., DENSE_RANK) or a correlated subquery counting higher distinct salaries.
- Be careful to include all employees who tie on a top‑three salary.
Approach 1 – Window Function (DENSE_RANK)
Partition by department and assign each salary a dense rank in descending order. Then filter to ranks 1 through 3.
SQL Query
SELECT DepartmentId, Name AS Employee, Salary FROM ( SELECT DepartmentId, Name, Salary, DENSE_RANK() OVER ( PARTITION BY DepartmentId ORDER BY Salary DESC ) AS rnk FROM Employee ) AS ranked WHERE rnk <= 3 ORDER BY DepartmentId, Salary DESC;
Step‑by‑Step
- The subquery assigns
rnk=1
to the highest salary in each department,rnk=2
to the next distinct salary, and so on. - We then select only those rows where
rnk≤3
. - Final
ORDER BY
ensures grouping by department and listing highest earners first.
Approach 2 – Correlated Subquery
For each employee, count how many distinct salaries in the same department exceed theirs. If fewer than 3, they are in the top three.
SQL Query
SELECT e1.DepartmentId, e1.Name AS Employee, e1.Salary FROM Employee AS e1 WHERE ( SELECT COUNT(DISTINCT e2.Salary) FROM Employee AS e2 WHERE e2.DepartmentId = e1.DepartmentId AND e2.Salary > e1.Salary ) < 3 ORDER BY e1.DepartmentId, e1.Salary DESC;
Step‑by‑Step
- For each
e1
, the subquery counts distinct salaries in its department that are strictly greater. - If that count is 0, 1, or 2, then
e1
’s salary ranks among the top three distinct. - Filter on that condition and sort the results.
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Complexity Analysis
- Both approaches run in O(N log N) time internally (sorting or windowing), where N is the number of employees.
- Window functions may use additional memory for partition buffers but generally scale well in modern RDBMS engines.
Common Mistakes
- Using ROW_NUMBER() instead of DENSE_RANK(), which would wrongly exclude ties.
- Forgetting
DISTINCT
in the correlated subquery count, causing over‑count when salaries repeat. - Omitting the final
ORDER BY
, leading to arbitrary output order.
Edge Cases
- Departments with fewer than three distinct salaries should still return all their employees.
- All employees in a department share the same salary.
- Single‐employee departments.
Alternative Variations
- Return only the top salary per department (MAX and GROUP BY).
- Find the employee with the 2nd highest salary in each department (
DENSE_RANK() = 2
). - List all employees whose salary is above the department average.
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.