SQL Flow Field Guide

LAST_VALUE

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

window analytic

What changes

Input
ordered rows of the partition (frame for FIRST_VALUE/LAST_VALUE; offset rows for LAG/LEAD)
Output or change
one value per row
Return type
The same type as scalar_expression.

Syntax at a glance

LAST_VALUE ( [ scalar_expression ] ) [ IGNORE NULLS | RESPECT NULLS ] OVER ( [ partition_by_clause ] order_by_clause [ rows_range_clause ] )

Work example

Show the final value in a chosen window frame.

Write the frame through UNBOUNDED FOLLOWING when each row should see the partition's final value.

Check your work The default frame often ends at the current peer group, so LAST_VALUE can otherwise mean the current value.

Practice SQL

Open in SQL Flow
SELECT n, LAST_VALUE(n) OVER (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_n FROM (VALUES (20), (10), (30)) AS v(n);

Open Microsoft Learn for full syntax, permissions, and details for your version.