SQLite Window Functions

An SQLite window function performs a calculation on a set of rows that are related to the current row. Unlike an aggregate function, a window function does not cause rows to become grouped into a single result row. a window function retains the row identities. Behind the scenes, window functions can access more than just the current row of the query result.

The following picture illustrates the differences between aggregate functions and window functions:

SQLite window function vs aggregate function

The window functions are divided into three categories: value window functions, ranking window functions, and aggregate window functions as shown in the following picture:

SQLite Window Functions

Window functions are also known as analytic functions. The following table shows all window functions supported by SQLite:

NameDescription
CUME_DISTCompute the cumulative distribution of a value in an ordered set of values.
DENSE_RANKCompute the rank for a row in an ordered set of rows with no gaps in rank values.
FIRST_VALUEGet the value of the first row in a specified window frame.
LAGProvide access to a row at a given physical offset that comes before the current row.
LAST_VALUEGet the value of the last row in a specified window frame.
LEADProvide access to a row at a given physical offset that follows the current row.
NTH_VALUEReturn the value of an expression evaluated against the row N of the window frame in the result set.
NTILEDivide a result set into a number of buckets as evenly as possible and assign a bucket number to each row.
PERCENT_RANKCalculate the percent rank of each row in an ordered set of rows.
RANKAssign a rank to each row within the partition of the result set.
ROW_NUMBERAssign a sequential integer starting from one to each row within the current partition.