T-SQL Tutorial

T-SQL FIRST_VALUE


The T-SQL FIRST_VALUE function is an analytic function and is used in SQL Server database to return the first value in an ordered set of values.

FIRST_VALUE Syntax


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



FIRST_VALUE Example


select b.*,
FIRST_VALUE(b.name) OVER (ORDER BY b.price ASC) AS FirstValue
from books b
order by b.price ASC;


select b.*,
FIRST_VALUE(b.name) OVER (ORDER BY b.price DESC) AS FirstValue
from books b
order by b.price DESC;