Why does LAST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary) return each row's own salary?
Scenario
A report should show every employee next to the highest salary in their department. The developer used LAST_VALUE ordered by salary, and every row shows its own salary instead.
Interview answerTry answering first, then reveal
Because of the default window frame. With an ORDER BY, the frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so the last row of the frame is the current row (or its ties). Extend the frame to UNBOUNDED FOLLOWING, or use MAX(salary) OVER (PARTITION BY dept) or FIRST_VALUE with a descending order.
Explanation
Every window function sees a frame: the rows from the partition it may look at for the current row. Ranking functions ignore the frame, but value functions such as LAST_VALUE, FIRST_VALUE and NTH_VALUE, and aggregates such as SUM, use it.
When OVER has an ORDER BY but no frame clause, the SQL standard default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. The frame grows row by row and ends at the current row, so LAST_VALUE returns the current value. With ties it returns the value of the last tied peer, which is the same salary.
FIRST_VALUE does not have this problem, because the frame always starts at the first row. That is why ordering descending and taking FIRST_VALUE is the common fix.
Worked example
Run on DuckDB when this page was built; the results shown are the real output.
Sample data
CREATE TABLE employees (emp_id INT, name VARCHAR, dept VARCHAR, salary INT);
INSERT INTO employees VALUES
(1, 'Asha', 'Engineering', 120), (2, 'Ben', 'Engineering', 110),
(3, 'Chen', 'Engineering', 110), (4, 'Dev', 'Engineering', 90),
(5, 'Eli', 'Sales', 95), (6, 'Fay', 'Sales', 80);The trap and three fixes
SELECT dept, name, salary,
LAST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary) AS last_default,
LAST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_full_frame,
FIRST_VALUE(salary) OVER (PARTITION BY dept ORDER BY salary DESC) AS first_desc,
MAX(salary) OVER (PARTITION BY dept) AS max_salary
FROM employees
ORDER BY dept, salary;| dept | name | salary | last_default | last_full_frame | first_desc | max_salary |
|---|---|---|---|---|---|---|
| Engineering | Dev | 90 | 90 | 120 | 120 | 120 |
| Engineering | Ben | 110 | 110 | 120 | 120 | 120 |
| Engineering | Chen | 110 | 110 | 120 | 120 | 120 |
| Engineering | Asha | 120 | 120 | 120 | 120 | 120 |
| Sales | Fay | 80 | 80 | 95 | 95 | 95 |
| Sales | Eli | 95 | 95 | 95 | 95 | 95 |
Only last_default is wrong. Ben and Chen both show 110 because they are tied peers.
In production
- Prefer
MAX/MINwithPARTITION BYwhen you only need the extreme value: it states the intent and needs no sort. - When you need another column from the top row (for example the top earner's name), use
FIRST_VALUE(name) OVER (... ORDER BY salary DESC, emp_id)with a tie-breaker. - The same default frame applies in PostgreSQL, SQL Server, Oracle, Snowflake, BigQuery and Spark SQL.
Common mistakes
- Adding
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWand expecting a different result: it still ends at the current row. - Assuming ties are broken in a stable way. Without a unique tie-breaker, which tied row is "first" can change between runs.
Related questions
More on this topic in the SQL Interview Questions guide, or browse all SQL questions.