SQLite 3 reference · Window Functions

SQLite NTH_VALUE() Function

Return a value from the Nth row of the window frame, or NULL if the frame has fewer rows.

SQLite 3 Since 3.25.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

nth_value(X, N) OVER (PARTITION BY ... ORDER BY ...)

Parameters

ParameterDescription
X A column or expression.
N The row number within the frame, from 1.

Return value

The value, or NULL.

Examples

The second-highest salaryTry it yourself
SELECT DISTINCT
       nth_value(name, 2) OVER (ORDER BY salary DESC
         ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS second_highest_paid
FROM employees;

Output

second_highest_paid
Omar Haddad
NULL until the frame reaches row 3Try it yourself
SELECT name, nth_value(name, 3) OVER (ORDER BY hire_date) AS third_hire
FROM employees
LIMIT 4;

Output

name | third_hire
Sofia Reyes | NULL
Omar Haddad | NULL
Hannah Okafor | Hannah Okafor
Mei Chen | Hannah Okafor

Version history

VersionChange
SQLite 3.25.0Added

In other languages

Was this page helpful?