Quick revision
The short answer
With ORDER BY in the window and no explicit frame, the frame runs from the start of the partition to the current row (and its peers), so the last value in the frame is the current row's price. Fix 1 is to widen the frame with ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING; fix 2 is FIRST_VALUE(price) OVER (PARTITION BY product_id ORDER BY changed_at DESC), which works with the default frame. If we only need one row per product, DISTINCT ON or a ROW_NUMBER filter is often cheaper.
What to remember
In this answer, we saw that LAST_VALUE(price) OVER (PARTITION BY product_id ORDER BY changed_at) returns the current row's price because the default frame ends at the current row and its peers. Widening the frame to ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, or using FIRST_VALUE over a descending order, gives the latest price on every row. When we need just one row per product, DISTINCT ON or a ROW_NUMBER filter is cheaper, and PostgreSQL's GROUPS mode and EXCLUDE options give finer control over frames.
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.
With ORDER BY in the window and no explicit frame, the frame runs from the start of the partition to the current row (and its peers), so the last value in the frame is the current row's price. Fix 1 is to widen the frame with ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING; fix 2 is FIRST_VALUE(price) OVER (PARTITION BY product_id ORDER BY changed_at DESC), which works with the default frame. If we only need one row per product, DISTINCT ON or a ROW_NUMBER filter is often cheaper.
Why this question is asked
This bug is famous because the query looks obviously right. LAST_VALUE of the price, ordered by time, should be the latest price, and yet every row comes back with its own price, as if the function did nothing. Interviewers like it because the explanation is the window frame, the part of window functions most developers never write and therefore never think about. In this answer, we will reproduce the bug, recap what the default frame is, see exactly why LAST_VALUE returns the current row, apply two fixes, look at cheaper alternatives when we need one row per product, and touch on PostgreSQL's extra frame options.
1Reproducing the Bug
Our price_history table records every price change. We want each row to show the latest price of its product next to it, for example to compare an old price with the current one.
Only the last row is right, and only because it is the last row. LAST_VALUE is working correctly; it is working over a frame that is smaller than we thought.
2A Quick Recap of the Default Frame
Every window function that looks at 'which rows' uses a frame: the subset of the ordered partition visible from the current row. When the OVER clause has ORDER BY but no frame clause, the SQL standard default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. In words: from the first row of the partition up to the current row, including any rows with the same changed_at as the current one, which are called its peers.
That default exists because it makes running totals work without extra typing: SUM(x) OVER (ORDER BY t) is a running sum. But it means the frame grows one row at a time as we move through the partition.
3Why LAST_VALUE Returns the Current Row
LAST_VALUE returns the value from the last row of the frame. With the default frame, the last row of the frame is the current row, or its last peer if several rows share the same changed_at. So for the 1 January row, the frame is just that row and LAST_VALUE gives 10.00. For the 1 March row, the frame is January and March, and the last of those is March.
| Current row | Rows in the default frame | LAST_VALUE | Wanted |
|---|---|---|---|
| 2025-01-01, 10.00 | Jan | 10.00 | 15.00 |
| 2025-03-01, 12.00 | Jan, Mar | 12.00 | 15.00 |
| 2025-06-01, 15.00 | Jan, Mar, Jun | 15.00 | 15.00 |
Notice the peer case too. If two price changes for product 7 had exactly the same changed_at, both rows would see the later of the two (in an arbitrary order), because RANGE includes peers. That makes the bug look intermittent on real data.
4Fix 1: Widen the Frame
We tell the function to look at the whole partition, from the first row to the last, whatever the current row is.
UNBOUNDED FOLLOWING moves the end of the frame to the last row of the partition, so the last value is the latest price for every row. If changed_at can repeat, we add a tiebreaker such as ORDER BY changed_at, id, so 'last' is well defined.
5Fix 2: Reverse the Order and Use FIRST_VALUE
The default frame always starts at the first row of the partition. So if we order newest first, the first row of every frame is the latest price, and FIRST_VALUE returns it without any frame clause.
This version is shorter and works on every engine with window functions. Both fixes give the same result; I usually prefer the explicit frame of fix 1 when the query has other window functions over the same ordering, so that they can share one WINDOW definition, and fix 2 when it stands alone. A named window helps either way: WINDOW w AS (PARTITION BY product_id ORDER BY changed_at ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING).
6When We Only Need One Row per Product
Often the real question is simply 'what is the latest price of each product', one row per product. A window over all history computes a value for every historical row only to throw most of them away. PostgreSQL's DISTINCT ON keeps the first row of each product in the given order, and with an index on (product_id, changed_at DESC) it reads very little.
Both still read all history for each product unless an index provides the order. For a large table with few products, a LATERAL join that takes ORDER BY changed_at DESC LIMIT 1 per product, or a current_price column maintained on the products table, is cheaper still.
7PostgreSQL's Extra Frame Options
PostgreSQL supports two frame features beyond ROWS and RANGE. GROUPS mode, since version 11, counts groups of peers rather than rows, so GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW means 'this timestamp and the previous distinct timestamp'. The EXCLUDE clause removes rows from the frame: EXCLUDE CURRENT ROW, EXCLUDE GROUP (the current row and its peers), EXCLUDE TIES (the peers but not the current row) or EXCLUDE NO OTHERS, the default. For example, AVG(price) OVER (PARTITION BY product_id ORDER BY changed_at ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING EXCLUDE CURRENT ROW) compares each price with its neighbours, which is handy for spotting outliers.
8Follow-up questions
Why does LAST_VALUE behave this way while SUM over the same window seems fine?
SUM over the default frame gives a running total, which is a common and useful result, so people rarely notice the frame. LAST_VALUE over the same growing frame gives the current row, which is almost never what anyone wants. The frame is the same in both cases; only the function makes the effect visible.
Does NTH_VALUE have the same problem?
Yes. NTH_VALUE(price, 2) over the default frame returns NULL for the first row, because the frame has only one row at that point. It needs the same wider frame to return the second price on every row.
Is ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING expensive?
Not much for LAST_VALUE and FIRST_VALUE, because the database can find the frame's end without scanning it for each row. Aggregates over an unbounded frame are computed once per partition. The main cost is still the sort by product_id and changed_at, which an index can remove.
9Conclusion
In this answer, we saw that LAST_VALUE(price) OVER (PARTITION BY product_id ORDER BY changed_at) returns the current row's price because the default frame ends at the current row and its peers. Widening the frame to ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, or using FIRST_VALUE over a descending order, gives the latest price on every row. When we need just one row per product, DISTINCT ON or a ROW_NUMBER filter is cheaper, and PostgreSQL's GROUPS mode and EXCLUDE options give finer control over frames.
Open with
The function is doing exactly what it's told; the frame is the problem. When a window has ORDER BY and no frame clause, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so it runs from the first row of the partition up to the current row and its peers.
Then, if they want more
The last value in that frame is the current row, so LAST_VALUE just returns each row's own price. That default exists so that SUM over ORDER BY gives a running total, which is why people don't notice it there. There are two fixes.
One is to widen the frame: ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, so every row sees the whole partition and the last value is the latest price.
The other is to flip the order and use FIRST_VALUE(price) OVER (PARTITION BY product_id ORDER BY changed_at DESC), because the default frame always starts at the first row, which is now the newest. In both cases I'd add a tiebreaker if changed_at can repeat.
If I only need the latest price per product, I'd use DISTINCT ON in PostgreSQL or a ROW_NUMBER equals one filter instead of computing a value on every historical row. PostgreSQL also has GROUPS frames and EXCLUDE options if I need finer control.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.0
- 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 "LAST_VALUE(price) OVER (PARTITION BY product_id ORDER BY changed_at) returns the current...": https://scrivio-interview-library.netlify.app/databases/advanced-sql/882c2a7dfe/