FIRST_VALUE is an analytic (window) function that lets you look "forward" in your result set to retrieve the first value in the current window frame. Unlike aggregate functions, it does not collapse rows; it produces a value for every row while keeping row-level detail. The function operates after the WHERE, GROUP BY, and HAVING clauses but before the final ORDER BY, making it ideal for ranking, cohort analysis, and time-series calculations. Its behavior depends on the window frame definition: if you specify ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, it will always return the absolute first value in the partition; if you use a sliding frame such as ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, the result can change from row to row. NULLs are treated as ordinary values and returned as-is. If the frame contains no rows (rare but possible with RANGE frames), FIRST_VALUE returns NULL. Performance is generally on par with other window functions, but large frames or poorly indexed ORDER BY columns can increase sort cost.
value_expression
(Any) - Column or expression whose first value you want.PARTITION BY
(Column list) - Separates the data into independent partitions. Optional.ORDER BY
(Column list) - Defines row order inside each partition and thereby which row is first. Required.ROWS
(RANGE clause) - Frame spec|||Defines the subset of rows considered for each calculation. Optional but highly recommended.LAST_VALUE, LAG, LEAD, ROW_NUMBER, MIN, WINDOW FRAME, PARTITION BY
SQL:2003 standard
It copies the first value in the window frame to every row within that frame, allowing easy comparisons to a baseline.
Yes. ORDER BY defines which row is first. Without it, most systems raise an error or use an undefined default order.
Add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to the window clause so the frame covers the entire partition.
FIRST_VALUE will return NULL. If you want the first non-NULL value, combine it with IGNORE NULLS (Oracle) or use a subquery/conditional expression.