SQLite Window Functions: ROW_NUMBER, RANK, LAG and More
SQLite window functions calculate values across related rows without collapsing those rows into a single result per group. Use them for rankings, previous or next values, and totals alongside individual records. Unlike a GROUP BY aggregate, a window aggregate keeps one output row for each input row.
Window function syntax
window_function(arguments) OVER (
PARTITION BY group_column
ORDER BY sort_column
)
OVERidentifies the window used by the function.PARTITION BYsplits rows into independent groups. Omit it to use the entire result set.ORDER BYcontrols the order within each partition, which matters for ranking and offset functions.- Some functions also accept a window frame such as
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. The default frame depends on the window specification; specify it explicitly when calculating running aggregates.
Example: rank rows and show group totals
WITH sales(team, amount) AS (
VALUES ('A', 10), ('A', 20), ('B', 15)
)
SELECT team,
amount,
ROW_NUMBER() OVER (
PARTITION BY team ORDER BY amount DESC
) AS row_num,
SUM(amount) OVER (PARTITION BY team) AS team_total
FROM sales
ORDER BY team, amount DESC;
| team | amount | row_num | team_total |
|---|---|---|---|
| A | 20 | 1 | 30 |
| A | 10 | 2 | 30 |
| B | 15 | 1 | 15 |
ROW_NUMBER() restarts at 1 for each team. SUM(...) OVER (...) repeats the team total beside each sale instead of returning only one row per team.
Choose a window function
ROW_NUMBER()assigns a unique sequential position within a partition.RANK()gives tied rows the same rank and leaves gaps after ties.DENSE_RANK()gives tied rows the same rank without gaps.LAG()andLEAD()access previous and next rows.FIRST_VALUE()andLAST_VALUE()read values from the window frame. ForLAST_VALUE(), check the frame bounds carefully.
Window functions require SQLite 3.25.0 or later. Try the query in the SQLite Playground. For calculations that should return one row per group instead, see SQLite aggregate functions.
-
cume_dist
The SQLitecume_dist()function returns the cumulative distribution of the current row. -
dense_rank
The SQLitedense_rank()function returns the rank within the partition in which the current row is located, starting at 1, with no gap. -
first_value
The SQLitefirst_value()function returns the evaluated value from the first row of the window frame associated with the current row. -
lag
The SQLitelag()function returns the value from the specified row before the current row in the partition where the current row is located. -
last_value
The SQLitelast_value()function returns the evaluated value from the last row of the window frame associated with the current row. -
lead
The SQLitelead()function returns the value from the specified row after the current row in the partition where the current row is located. -
nth_value
The SQLitenth_value()function returns the evaluated value from the specified row of the window frame associated with the current row. -
ntile
The SQLitentile()function divides all rows in the partition into the specified number of buckets as evenly as possible, and returns the rank of the bucket where the current row is located. -
percent_rank
The SQLitepercent_rank()function returns the relative rank within the partition where the current row is located, that is(rank() - 1) / (number of rows partition - 1). -
rank
The SQLiterank()function returns the rank within the partition in which the current row is located, starting at 1, with gaps. -
row_number
Learn SQLite ROW_NUMBER() with runnable PARTITION BY and ORDER BY examples, deterministic tie-breaking, top N per group, and its difference from RANK().