Skip to main content

Window Functions

Window functions perform calculations across a set of rows that are related to the current row. Unlike aggregate functions with GROUP BY, window functions do not collapse rows into a single output row. Every input row produces a corresponding output row, with the window function result appended.
Turso supports aggregate functions used as window functions with the default frame definition, the row_number() ranking function, and the FILTER (WHERE ...) clause. The remaining dedicated window functions (rank, dense_rank, ntile, lag, lead, first_value, last_value, nth_value) and custom frame specifications (ROWS, RANGE, or GROUPS with explicit bounds) are not yet supported.

Syntax

Default Frame

When ORDER BY is specified, the default frame is:
This means the function considers all rows from the start of the partition up to and including the current row (and any rows with equal ORDER BY values, since the frame mode is RANGE). When ORDER BY is omitted, the default frame covers the entire partition.

Supported Aggregate Functions as Window Functions

Any aggregate function can be used as a window function by adding an OVER clause.

Ranking Functions

row_number()

Assigns a sequential integer (starting at 1) to each row within its partition, in the order defined by the window’s ORDER BY. This is the one dedicated ranking function supported in Turso.

FILTER (WHERE …)

A FILTER (WHERE ...) clause restricts which rows an aggregate window function includes, without affecting the rows returned by the query.

PARTITION BY

PARTITION BY divides the rows into groups. The window function resets and recalculates independently for each partition.
Without PARTITION BY, the function treats the entire result set as one partition:

ORDER BY

ORDER BY within the OVER clause determines row ordering within each partition. Combined with the default frame (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), this produces running calculations.

PARTITION BY with ORDER BY

Use both clauses together for running calculations within groups:

Named Windows

The WINDOW clause defines a reusable window specification that can be referenced by multiple window functions in the same query. This avoids repeating the same OVER definition.
Multiple named windows can be defined:

Examples

Running Total

Count per Group

Running Average

Percentage of Total

Multiple Window Functions in One Query

Group Concatenation over a Window

Limitations

The following window function features are not yet supported in Turso: row_number() and FILTER (WHERE ...) are supported — see the sections above.

See Also