Sub topic 12
FIRST_VALUE, LAST_VALUE and NTH_VALUE
Assume you know SELECT, GROUP BY, aggregate functions, common table expressions (CTEs), and the OVER clause. A window function calculates a value from related rows while keeping the current row in the result. This chapter focuses on value functions, which return a value from a particular position in each window.
The three value functions
FIRST_VALUE(expression)returns the expression from the first row in the window.LAST_VALUE(expression)returns the expression from the last row in the window.NTH_VALUE(expression, n)returns the expression from the nth row in the window.
The window's PARTITION BY determines which rows belong together. Its ORDER BY determines their positions. The frame clause determines which portion of that ordered partition the function can see.
Consider this sales table:
| region | sale_date | salesperson | amount |
|---|---|---|---|
| East | 2026-01-03 | Ana | 120 |
| East | 2026-01-05 | Bo | 200 |
| East | 2026-01-08 | Cy | 150 |
The following query compares every sale with the first and second sale in its region:
SELECT
region,
sale_date,
salesperson,
amount,
FIRST_VALUE(amount) OVER (
PARTITION BY region
ORDER BY sale_date
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS first_amount,
NTH_VALUE(amount, 2) OVER (
PARTITION BY region
ORDER BY sale_date
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS second_amount
FROM sales
ORDER BY region, sale_date;UNBOUNDED PRECEDING means the partition's first row. UNBOUNDED FOLLOWING means its last row. The explicit frame lets both functions see the complete region, so second_amount is 200 on every East row.
The LAST_VALUE frame trap
LAST_VALUE often surprises people because an ordered window commonly has a default frame that ends at the current row. In that case, the last visible row is usually the current row, not the last row in the partition.
SELECT
region,
sale_date,
amount,
LAST_VALUE(amount) OVER (
PARTITION BY region
ORDER BY sale_date
) AS misleading_last_amount,
LAST_VALUE(amount) OVER (
PARTITION BY region
ORDER BY sale_date
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS actual_last_amount
FROM sales
ORDER BY region, sale_date;For the East rows, misleading_last_amount is 120, 200, and 150. Each row sees itself as the last row in its default frame. actual_last_amount is 150 for all three rows.
Ordering and missing values
Add a unique column, such as id, after the date when dates can repeat:
ORDER BY sale_date, idWithout a deterministic order, the database may choose different rows as first, second, or last.
Value functions can also return NULL. For example, FIRST_VALUE(customer_name) returns NULL if the first ordered row has no customer name. Support for options such as IGNORE NULLS differs among database systems, so check your database documentation before relying on them.
Choosing the right function
Use FIRST_VALUE for a baseline or opening value, such as the first purchase in an account. Use LAST_VALUE for a final or closing value, but specify the frame when you mean the whole partition. Use NTH_VALUE when a fixed position matters, such as the third event in a session.
These functions do not find the minimum or maximum value. For that, use MIN or MAX. The “first” and “last” rows are determined by the window's ordering, not by the numeric size of the expression.
Exercise
What frame should you add when LAST_VALUE must return the final amount in the entire region?
Show answer
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.