Skip to main content

Overview

The FIRST_VALUE() is a window function that retrieves the first value in an ordered set of values within a specified partition.

Syntax

The syntax for this function is as follows:

Parameters

  • expression: target column or expression that the function operates on
  • PARTITION BY: optional clause, which divides the result set into partitions to which the FIRST_VALUE() function is applied (if omitted, the entire result set is treated as a single partition)
  • ORDER BY: order of rows in each partition to which the function is applied
  • RANGE BETWEEN: range-based window frame relative to the current row

Example

For the needs of this section, we will use a simplified version of the film table from the Pagila database, containing only the title, length and rating columns. The complete schema for the film table can be found on the Pagila database website.
The query below uses the FIRST_VALUE() function to retrieve the title of the film with the shortest duration, partitioning results by rating and ordering by length.
By executing the code above, we will get the following output: