FIRST_VALUE
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
FIRST_VALUE ( [ scalar_expression ] ) [ IGNORE NULLS | RESPECT NULLS ] OVER ( [ partition_by_clause ] order_by_clause [ rows_range_clause ] )
Work example
Show the first value in each ordered window, such as a starting balance.
Set the window frame explicitly when the intended first value must remain fixed across the partition.
Check your work The ORDER BY and frame together define which row is first.
Practice SQL
Open in SQL FlowSELECT n, FIRST_VALUE(n) OVER (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS first_n FROM (VALUES (20), (10), (30)) AS v(n);
Open Microsoft Learn for full syntax, permissions, and details for your version.