SQLite 3 reference · Window Functions

SQLite FIRST_VALUE() Function

Return a value from the first row of the window frame, such as each department's highest salary.

SQLite 3 Since 3.25.0

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

Syntax

first_value(X) OVER (PARTITION BY ... ORDER BY ...)

Parameters

ParameterDescription
X A column or expression.

Return value

The value from the frame's first row.

Examples

The top of each partitionTry it yourself
SELECT department, name, salary,
       first_value(name) OVER (PARTITION BY department ORDER BY salary DESC) AS top_earner
FROM employees
WHERE department IN ('Sales', 'Support');

Output

department | name | salary | top_earner
Sales | Hannah Okafor | 150000 | Hannah Okafor
Sales | Diego Silva | 90000 | Hannah Okafor
Sales | Emma Novak | 88000 | Hannah Okafor
Support | Ravi Kumar | 65000 | Ravi Kumar
Compare with the first rowTry it yourself
SELECT name, salary,
       salary - first_value(salary) OVER (ORDER BY salary) AS above_lowest
FROM employees
LIMIT 3;

Output

name | salary | above_lowest
Ravi Kumar | 65000 | 0
Emma Novak | 88000 | 23000
Diego Silva | 90000 | 25000

Version history

VersionChange
SQLite 3.25.0Added

In other languages

Was this page helpful?