GROUP BY, window functions do not collapse rows — every input row produces an output row with the window function result appended.
Syntax
| Clause | Description |
|---|---|
aggregate_function | Any supported aggregate: count, sum, avg, min, max, total, group_concat |
OVER (...) | Defines the window over which the function operates |
PARTITION BY | Divides the result set into partitions; the function resets for each partition. If omitted, the entire result set is one partition |
ORDER BY | Orders rows within each partition. Determines which rows fall within the default frame |
Default Frame
WhenORDER BY is specified inside OVER, the default frame is:
ORDER BY values (RANGE semantics).
When ORDER BY is omitted, the frame covers the entire partition.
Supported Functions
Any standard aggregate function can be used as a window function by adding anOVER clause:
| Function | Description |
|---|---|
count(*) | Number of rows in the frame |
count(expression) | Number of non-NULL values in the frame |
sum(expression) | Sum of non-NULL values in the frame |
avg(expression) | Average of non-NULL values in the frame |
min(expression) | Minimum value in the frame |
max(expression) | Maximum value in the frame |
total(expression) | Sum as REAL; returns 0.0 for empty frames instead of NULL |
group_concat(expression, separator) | Concatenation of values in the frame |
PARTITION BY
PARTITION BY divides rows into groups. The window function resets and recalculates independently within each partition.
| department | name | salary | dept_total |
|---|---|---|---|
| Engineering | Alice | 90000 | 250000 |
| Engineering | Bob | 85000 | 250000 |
| Engineering | Carol | 75000 | 250000 |
| Marketing | Dave | 70000 | 130000 |
| Marketing | Eve | 60000 | 130000 |
PARTITION BY, the entire result set is one partition:
ORDER BY Inside OVER
ORDER BY inside the OVER clause determines row ordering within partitions. Combined with the default frame, this produces running calculations:
| name | salary | running_total |
|---|---|---|
| Eve | 60000 | 60000 |
| Dave | 70000 | 130000 |
| Carol | 75000 | 205000 |
| Bob | 85000 | 290000 |
| Alice | 90000 | 380000 |
PARTITION BY with ORDER BY
| department | name | salary | dept_running_total |
|---|---|---|---|
| Engineering | Carol | 75000 | 75000 |
| Engineering | Bob | 85000 | 160000 |
| Engineering | Alice | 90000 | 250000 |
| Marketing | Eve | 60000 | 60000 |
| Marketing | Dave | 70000 | 130000 |
Named Windows
TheWINDOW clause defines a reusable window specification to avoid repeating the same OVER definition:
Examples
Running Total
Department Headcount
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:| Feature | Status |
|---|---|
row_number() | Not supported |
rank() | Not supported |
dense_rank() | Not supported |
ntile(N) | Not supported |
lag(expr, offset, default) | Not supported |
lead(expr, offset, default) | Not supported |
first_value(expr) | Not supported |
last_value(expr) | Not supported |
nth_value(expr, N) | Not supported |
cume_dist() | Not supported |
percent_rank() | Not supported |
Custom frame: ROWS BETWEEN ... | Not supported |
Custom frame: RANGE BETWEEN expr AND expr | Not supported |
Custom frame: GROUPS BETWEEN ... | Not supported |
EXCLUDE clause | Not supported |
FILTER (WHERE ...) on window functions | Not supported |
See Also
- Aggregate Functions — aggregate function reference
- SELECT — full SELECT syntax including the WINDOW clause
- Expressions — using window function results in expressions