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. Custom frame specifications (ROWS, RANGE, or GROUPS with explicit bounds) and dedicated window functions (row_number, rank, dense_rank, ntile, lag, lead, first_value, last_value, nth_value) 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.

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:

See Also