Window functions compute values across a set of table rows related to the current row — similar to aggregate functions, but without collapsing the result into a single row. TiDB supports the full SQL window function syntax including theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/pingcap/tidb/llms.txt
Use this file to discover all available pages before exploring further.
OVER clause, PARTITION BY, ORDER BY, and frame specifications.
Syntax
| Clause | Description |
|---|---|
PARTITION BY | Divides rows into independent groups (like GROUP BY, but rows are not collapsed) |
ORDER BY | Defines the row order within each partition |
frame_clause | Further restricts which rows within the partition are included in the calculation |
Ranking functions
Ranking functions assign ordinal positions to rows within a partition.Sample data
ROW_NUMBER()
Assigns a unique sequential integer to each row within a partition.| rep | region | amount | row_num |
|---|---|---|---|
| Carol | East | 11500.00 | 1 |
| Dave | East | 9800.00 | 2 |
| Eve | East | 9800.00 | 3 |
| Alice | West | 9200.00 | 1 |
| Bob | West | 8100.00 | 2 |
RANK()
LikeROW_NUMBER(), but rows with equal values receive the same rank and the next rank is skipped.
| rep | region | amount | rnk |
|---|---|---|---|
| Carol | East | 11500.00 | 1 |
| Dave | East | 9800.00 | 2 |
| Eve | East | 9800.00 | 2 |
| Alice | West | 9200.00 | 1 |
| Bob | West | 8100.00 | 2 |
DENSE_RANK()
LikeRANK(), but does not skip ranks after ties.
NTILE(n)
Divides rows inton approximately equal buckets.
Value functions
Value functions return a value from a specific row relative to the current row.LAG() and LEAD()
LAG(expr, offset, default) returns a value from a preceding row; LEAD() looks ahead.
FIRST_VALUE() and LAST_VALUE()
Return the first or last value in the window frame.LAST_VALUE() uses the default frame (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) unless you explicitly extend it with ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.Aggregate window functions
Standard aggregate functions (SUM, AVG, COUNT, MIN, MAX) can be used as window functions by adding an OVER clause.
Running total
| rep | amount | running_total |
|---|---|---|
| Alice | 9200.00 | 9200.00 |
| Bob | 8100.00 | 17300.00 |
| Carol | 11500.00 | 28800.00 |
| Dave | 9800.00 | 38600.00 |
| Eve | 9800.00 | 48400.00 |
Moving average (3-row window)
Partition aggregate
Frame clauses
A frame restricts which rows within the partition are included in the window calculation.| Syntax | Description |
|---|---|
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW | All rows from the start of the partition to the current row |
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW | The current row and the two preceding rows |
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING | A sliding 3-row window centered on the current row |
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING | All rows in the partition |
ROWS counts physical rows; RANGE counts rows with values within a range of the current row’s ORDER BY value. Use ROWS when working with numeric offsets; use RANGE when working with ordered values and ties matter.
Common use cases
Identify the top-N per group
Identify the top-N per group
Calculate month-over-month growth
Calculate month-over-month growth
Assign row numbers for pagination
Assign row numbers for pagination