Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A developer writes:

SQL
SELECT DISTINCT dept_id, NTH_VALUE(salary, 2) OVER (PARTITION BY dept_id
ORDER BY salary DESC) AS second
FROM employee;

It returns two rows for some departments, one with NULL. Explain why and fix it, and compare with a solution based on DENSE_RANK.

With ORDER BY in the window, the default frame runs from the start of the partition only to the current row and its peers, so for the top-salary rows NTH_VALUE(salary, 2) sees one value and returns NULL, while lower rows return the real value; DISTINCT then shows both. Fix it with an explicit frame, ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, and remember that NTH_VALUE counts rows, not distinct values, so for the second distinct salary DENSE_RANK = 2 is the right tool.

Why this question is asked

Window frames are the part of window functions most developers never learn on purpose, and they cause exactly this kind of bug: a query that looks right and returns plausible numbers mixed with NULLs. Interviewers ask it because explaining it requires knowing what the default frame is and why it exists. In this answer, we will recap partitions, ordering and frames, reproduce the two rows, fix the frame, see why NTH_VALUE still answers a different question than DENSE_RANK, and note the database differences.

1A Quick Recap of Window Frames

A window function computes a value for each row from a set of related rows. PARTITION BY decides which rows are related, here all rows of the same department. ORDER BY sorts them. The frame then decides which rows of that sorted partition the function actually looks at for the current row.

The trap is the default. When the window has an ORDER BY but no frame clause, the standard default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: from the first row of the partition up to the current row and all its peers, rows with the same ORDER BY value. This default exists so that SUM(x) OVER (ORDER BY d) gives a running total. Without ORDER BY the frame is the whole partition.

2Reproducing the Two Rows

Take department 10 with salaries 120, 100 and 90. The rows are sorted by salary descending, and for each row NTH_VALUE looks for the second row in its frame.

SQL
SELECT dept_id, salary,
NTH_VALUE(salary, 2) OVER (PARTITION BY dept_id ORDER BY salary DESC) AS second
FROM employee;
-- 10 | 120 | NULL frame: {120} no second row yet
-- 10 | 100 | 100 frame: {120, 100}
-- 10 | 90 | 100 frame: {120, 100, 90}
SELECT DISTINCT dept_id,
NTH_VALUE(salary, 2) OVER (PARTITION BY dept_id ORDER BY salary DESC) AS second
FROM employee;
-- 10 | NULL
-- 10 | 100
Frame per row for salaries 120, 100, 90
RowDefault frameNTH_VALUE(salary, 2)
120120NULL
100120, 100100
90120, 100, 90100

The first row's frame ends at itself, so there is no second row and the result is NULL. Every later row's frame is large enough, so it gets 100. DISTINCT removes the duplicate 100 but keeps the NULL, which is the second row the developer saw.

Notice the role of peers. In a department with 100, 100 and 90, the first row's frame already includes both 100 rows because they are peers under RANGE, so no NULL appears there, and that is why only some departments show two rows.

3Fixing the Frame

We want every row to see the whole department, so we state the frame explicitly.

SQL
SELECT DISTINCT dept_id,
NTH_VALUE(salary, 2) OVER (
PARTITION BY dept_id ORDER BY salary DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS second
FROM employee;
-- 10 | 100

Now every row of department 10 gets the same value, so DISTINCT leaves one row per department. A department with a single employee returns NULL, because its frame has no second row, which is a sensible result. In PostgreSQL and MySQL we can also declare the window once with a WINDOW clause and reuse it, which keeps longer queries readable.

4Rows Are Not Distinct Values

The fixed query is still not the second highest distinct salary. NTH_VALUE counts rows in the frame. With salaries 100, 100 and 90, the second row is the second 100, so NTH_VALUE returns 100, even though the second distinct salary is 90.

Java
-- department 20: 100, 100, 90
-- NTH_VALUE with the full frame -> 100
SELECT dept_id, MAX(salary) AS second_distinct
FROM (SELECT dept_id, salary,
DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
FROM employee WHERE salary IS NOT NULL) t
WHERE rnk = 2
GROUP BY dept_id;
-- 20 | 90

DENSE_RANK gives both 100s rank 1 and 90 rank 2, so rank 2 is the second distinct value. MAX collapses ties into one value per department. So the two functions answer different questions: NTH_VALUE answers what is the salary of the second person in this order, and DENSE_RANK answers what is the second distinct salary. The question should decide which one we use, and most second highest salary questions mean the distinct one.

5Database Differences

Both MySQL 8 and PostgreSQL support NTH_VALUE with the same default frame behaviour. The SQL standard also allows FROM FIRST or FROM LAST, to count from either end of the frame, and RESPECT NULLS or IGNORE NULLS. FROM FIRST is the default everywhere. PostgreSQL does not support FROM LAST, and MySQL accepts only FROM FIRST in practice, so check your version before relying on either option; reversing the ORDER BY gives the same effect as FROM LAST.

6Follow-up questions

Which other window functions are affected by the default frame?

Every function that works on the frame: LAST_VALUE, NTH_VALUE and aggregates such as SUM, AVG, MIN, MAX and COUNT used with OVER (ORDER BY ...). LAST_VALUE is the famous case, because with the default frame it returns the current row's value, or its last peer, instead of the partition's last value. FIRST_VALUE usually looks fine because the default frame starts at the partition's first row. Ranking functions such as ROW_NUMBER, RANK and DENSE_RANK and the offset functions LAG and LEAD ignore the frame entirely.

What is the difference between ROWS and RANGE in a frame?

ROWS counts physical rows, so CURRENT ROW means exactly this row. RANGE works on the ORDER BY values, so CURRENT ROW includes all peers with the same value. For running totals with duplicate values, RANGE adds all tied rows at once, while ROWS adds them one by one.

Why not just use MAX over the partition and filter below it?

That works for the second highest: the maximum salary below the partition maximum is the second distinct value. It is fine for second, but DENSE_RANK generalises to the third or tenth without nesting.

7Conclusion

In this answer, we saw that a window with ORDER BY and no frame clause uses RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so NTH_VALUE returns NULL for rows that come before the second row, and DISTINCT exposes both values. An explicit full frame fixes it, but NTH_VALUE still counts rows, so for the second distinct salary DENSE_RANK = 2 with MAX is the right answer. FROM LAST is not available in PostgreSQL.

What to say in the interviewAbout 90 seconds aloud

Open with

The cause is the default window frame. When a window has ORDER BY and no frame clause, the frame is RANGE from unbounded preceding to the current row, which is what makes running totals work.

Then, if they want more

So for each row, NTH_VALUE of salary two only sees the rows from the top of the department down to that row and its peers. For the top salary row, there is only one row in the frame, so it returns NULL; for later rows it returns the real second value.

DISTINCT then keeps both the NULL and the value, which is why some departments show two rows. Departments whose top salary is tied do not show it, because tied rows are peers and are already in the first frame.

The fix is an explicit frame, ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, so every row sees the whole department and returns the same value. But I would point out that NTH_VALUE counts rows, not distinct values: with salaries 100, 100 and 90 it returns 100.

If the question means the second highest distinct salary, I would use DENSE_RANK partitioned by department ordered by salary descending, keep rank two and take MAX per department, filtering NULL salaries first. Both MySQL 8 and PostgreSQL support NTH_VALUE; FROM FIRST is the default, and PostgreSQL does not support FROM LAST.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8
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.
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 "A developer writes:": https://scrivio-interview-library.netlify.app/databases/sql-coding-problems/8e468e05bc/