SQLite 3 reference · Window Functions

SQLite LAST_VALUE() Function

Return a value from the last row of the window frame. Widen the frame to see the real last row of the partition.

SQLite 3 Since 3.25.0

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

Syntax

last_value(X) OVER (... ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

Parameters

ParameterDescription
X A column or expression.

Return value

The value from the frame's last row.

Examples

Why the frame mattersTry it yourself
SELECT name, salary,
       last_value(name) OVER (ORDER BY salary) AS default_frame,
       last_value(name) OVER (ORDER BY salary
         ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS whole_partition
FROM employees
WHERE department = 'Sales';

Output

name | salary | default_frame | whole_partition
Emma Novak | 88000 | Emma Novak | Hannah Okafor
Diego Silva | 90000 | Diego Silva | Hannah Okafor
Hannah Okafor | 150000 | Hannah Okafor | Hannah Okafor
Latest hire per departmentTry it yourself
SELECT DISTINCT department,
       last_value(hire_date) OVER (PARTITION BY department ORDER BY hire_date
         ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS latest_hire
FROM employees;

Output

department | latest_hire
Engineering | 2023-08-28
Management | 2018-03-01
Sales | 2024-01-08
Support | 2024-10-01

Common mistakes

  • With an ORDER BY, the default frame ends at the current row, so last_value() returns the current row's value. Add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

Version history

VersionChange
SQLite 3.25.0Added

In other languages

Was this page helpful?