Quick revision
The short answer
Rank salaries inside each department with DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC), keep rank 2 inside the subquery or the ON clause, and LEFT JOIN that result from department so every department appears, with NULL when there is no second distinct salary. Wrap the value in MAX and group by department, because several employees can share the second salary.
What to remember
In this answer, we ranked salaries per department with DENSE_RANK so rank 2 is the second distinct value, attached the rank 2 rows to department with a LEFT JOIN and the filter in the ON clause so departments without a second salary or without employees show NULL, and collapsed ties with MAX. We compared it with the aggregate version based on the maximum, and saw why NULL salaries must be filtered because PostgreSQL sorts them first in descending order.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Rank salaries inside each department with DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC), keep rank 2 inside the subquery or the ON clause, and LEFT JOIN that result from department so every department appears, with NULL when there is no second distinct salary. Wrap the value in MAX and group by department, because several employees can share the second salary.
Why this question is asked
Second highest is a classic because it has several traps in a short question: distinct values versus rows, ties, departments with too few employees, departments with none, and the difference between filtering in ON and in WHERE. Interviewers often follow up with exactly that last point. In this answer, we will recap the ranking functions, build the ranked query, attach it to departments so empty ones survive, compare it with an aggregate-only version, and cover ties and NULL salaries.
1A Quick Recap of ROW_NUMBER, RANK and DENSE_RANK
All three number rows inside a partition in a chosen order, and they differ only in ties. ROW_NUMBER gives 1, 2, 3 even when values repeat. RANK gives tied rows the same number and then skips, so 100, 100, 90 become 1, 1, 3. DENSE_RANK gives tied rows the same number and does not skip, so 100, 100, 90 become 1, 1, 2.
The question asks for the second highest distinct salary, so DENSE_RANK is the right tool: rank 2 is always the second distinct value, however many people share the top salary.
| salary | ROW_NUMBER | RANK | DENSE_RANK |
|---|---|---|---|
| 100 | 1 | 1 | 1 |
| 100 | 2 | 1 | 1 |
| 90 | 3 | 3 | 2 |
2Ranking Inside Each Department
We partition by dept_id so the numbering restarts for every department, and order by salary descending so the highest salary gets rank 1. We also drop NULL salaries first, for a reason we will see below.
3Keeping Every Department With a LEFT JOIN
Department 20 has only one distinct salary, and department 30 has no employees at all, yet both must appear with NULL. So we start from department, keep it on the preserved side of a LEFT JOIN, and attach only the rank 2 rows.
- LEFT JOIN department to ranked salaries
- Filter rnk = 2 in ON or the subquery?Yesunmatched departments kept with NULLNoWHERE rnk = 2 drops them
- GROUP BY department, MAX(salary)ties collapse to one value
3 steps in order. Play them, or step through them one at a time.
Notice where r.rnk = 2 lives: in the ON clause. If we moved it to a WHERE clause after the LEFT JOIN, Legal and Research would disappear. For them the join found no rank 2 row, so r.rnk is NULL in their output rows, NULL = 2 is unknown, and WHERE throws the rows away. The LEFT JOIN would have kept them, and the WHERE would undo it.
The MAX with GROUP BY handles ties. If two Sales employees earned 90, the join would return two rows for Sales, and MAX collapses them into one value. If we wanted the employees and not only the value, we would drop the MAX and accept several rows.
4The Aggregate-Only Alternative
Without window functions, the second highest distinct salary is the highest salary below the maximum. A correlated subquery finds each department's maximum.
It is correct for distinct values, since anything below the maximum excludes all the tied top salaries. But on its own it only returns departments that have a second salary, so we still LEFT JOIN it from department to show the NULLs. It also does not generalise well: the third highest needs another nested level, while the ranked version just changes 2 to 3.
5Ties and NULL Salaries
Ties at the second salary are handled by MAX, as we saw. NULL salaries need care for a less obvious reason: in PostgreSQL, ORDER BY salary DESC puts NULLs first by default, so a NULL salary would get rank 1 and the real highest salary would become rank 2. MySQL sorts NULLs last in descending order, so the same query gives different answers on the two databases.
Filtering WHERE salary IS NOT NULL in the subquery, or writing ORDER BY salary DESC NULLS LAST in PostgreSQL, removes that difference. It is a small detail, but it is exactly the kind of thing that distinguishes a tested answer.
6Follow-up questions
Why does putting r = 2 in WHERE after a LEFT JOIN drop departments?
WHERE runs after the join. For a department with no rank 2 row, the LEFT JOIN produces a row with NULLs in every column from the ranked side, including the rank. The condition NULL = 2 is unknown, WHERE keeps only true rows, so the department is removed and the LEFT JOIN behaves like an inner join. Conditions on the optional side belong in the ON clause or inside the subquery.
How would you return the employees who earn the second highest salary?
Use the same ranked subquery but select the employee columns and filter rnk = 2, without MAX. Departments with ties then return several employees, which is what we want for that question.
Would ROW_NUMBER work instead of DENSE_RANK?
Not for distinct salaries. With 100, 100, 90, ROW_NUMBER gives the second 100 the number 2, so it returns 100 as the second highest, which is the second row, not the second value.
7Conclusion
In this answer, we ranked salaries per department with DENSE_RANK so rank 2 is the second distinct value, attached the rank 2 rows to department with a LEFT JOIN and the filter in the ON clause so departments without a second salary or without employees show NULL, and collapsed ties with MAX. We compared it with the aggregate version based on the maximum, and saw why NULL salaries must be filtered because PostgreSQL sorts them first in descending order.
Open with
I would rank salaries per department with DENSE_RANK over PARTITION BY dept_id ORDER BY salary DESC. DENSE_RANK is the right function because tied salaries share a rank without gaps, so rank two is always the second distinct value; ROW_NUMBER would return the second row, which might be a tied top salary.
Then, if they want more
Because every department must appear, I start from the department table and LEFT JOIN the ranked subquery, putting rank equals two in the ON clause. If I put it in WHERE, departments without a match would have a NULL rank, the condition would be unknown, and they would disappear.
Then I group by department and take MAX of the salary, which turns several employees tied at the second salary into one value and returns NULL for departments with fewer than two distinct salaries or no employees.
An alternative without window functions is the maximum salary below the department's maximum, using a correlated subquery, but it still needs the LEFT JOIN from department and does not generalise to the third highest as easily. Finally, I filter out NULL salaries in the subquery, because in PostgreSQL descending order puts NULLs first, so a NULL would take rank one.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Using employee(id, name, dept_id, salary) and department(id, name), return each...": https://scrivio-interview-library.netlify.app/databases/sql-coding-problems/c25afb0af5/