Window functions
ROW_NUMBER, RANK, DENSE_RANK, NTILE, LAG, LEAD, FIRST_VALUE, LAST_VALUE, NTH_VALUE and the aggregates with OVER, with the window and frame syntax.
A window function computes a value for each row from a set of rows related to it (its window), without collapsing the rows as GROUP BY does.
Syntax#
function(args) OVER { window_name | ( [window_name] [PARTITION BY expr [, expr] ...]
[ORDER BY expr [ASC | DESC] [, ...]]
[frame] ) }
frame: { ROWS | RANGE } { frame_start | BETWEEN frame_start AND frame_end }
frame_start, frame_end: { UNBOUNDED PRECEDING | n PRECEDING | CURRENT ROW | n FOLLOWING | UNBOUNDED FOLLOWING }
SELECT ... FROM ... WINDOW w AS ( window specification ) [, w2 AS (...)] ...Remarks#
- A frame can be
ROWSorRANGE;GROUPSframes are not supported yet. Without a frame and with anORDER BY, the frame is the rows from the start of the partition to the current row and its peers. - A window function can be used in the select list and in
ORDER BYonly; the rows it needs are produced first, soWHERE ROW_NUMBER() OVER (...) = 1is refused as in MySQL (filter in an outer query: see the example below). - A window name that is not defined by
WINDOWis an error. - The same window specification gives the same results in the MySQL and the MariaDB dialect.
CUME_DIST,PERCENT_RANK,NTILE,NTH_VALUE,LAG,LEADfollow the SQL standard definitions.
The examples use the table e that gives the same output as MySQL 9.7:
CREATE TABLE e (id INT PRIMARY KEY, dept INT, name VARCHAR(10), sal INT);
INSERT INTO e VALUES (1, 1, 'ann', 100), (2, 1, 'bob', 200), (3, 2, 'cy', 150), (4, 2, 'di', 150), (5, 2, 'ed', 50), (6, NULL, 'fay', 70);The first row of each department by salary, using a window function in a derived table:
SELECT * FROM (SELECT id, name, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY sal DESC) rn FROM e) x WHERE rn = 1 ORDER BY id;A named window used twice:
SELECT id, RANK() OVER w r, SUM(sal) OVER w s FROM e WINDOW w AS (PARTITION BY dept ORDER BY sal) ORDER BY id;A window function over an aggregate:
SELECT dept, SUM(sal) s, RANK() OVER (ORDER BY SUM(sal) DESC) rk FROM e GROUP BY dept ORDER BY dept;Summary of functions#
| Function | What it does | Dialect |
|---|---|---|
AVG, COUNT, MAX, MIN, SUM | The aggregate functions used as window functions: computed over the rows of the window frame of each row instead of collapsing the rows. | MySQL and MariaDB |
CUME_DIST | Returns the share of the rows of the partition that are before or equal to the current row in the window order (peers included), a number from above 0 to 1. | MySQL and MariaDB |
DENSE_RANK | Returns the rank of the current row among the peers of the partition, without gaps. | MySQL and MariaDB |
FIRST_VALUE | Returns expr of the first row of the window frame. | MySQL and MariaDB |
LAG | Returns expr of the row offset rows (1 by default) before the current row in the partition, or default (NULL by default) if there is no such row. | MySQL and MariaDB |
LAST_VALUE | Returns expr of the last row of the window frame. | MySQL and MariaDB |
LEAD | Returns expr of the row offset rows (1 by default) after the current row in the partition, or default if there is no such row. | MySQL and MariaDB |
NTH_VALUE | Returns expr of the n-th row of the window frame, or NULL if the frame has fewer rows. | MySQL and MariaDB |
NTILE | Divides the partition into n groups as equal as possible and returns the number of the group of the current row. | MySQL and MariaDB |
PERCENT_RANK | Returns (rank - 1) / (rows in the partition - 1), a number from 0 to 1. | MySQL and MariaDB |
RANK | Returns the rank of the current row in the partition; peers (rows equal in the window order) have the same rank and the next rank is skipped. | MySQL and MariaDB |
ROW_NUMBER | Returns the number of the current row in its partition, starting at 1, in the window order. | MySQL and MariaDB |
AVG, COUNT, MAX, MIN, SUM#
{AVG | COUNT | MAX | MIN | SUM}(expr) OVER (...)The aggregate functions used as window functions: computed over the rows of the window frame of each row instead of collapsing the rows. The other aggregates (GROUP_CONCAT, STD*, VAR*, BIT_*) are not read as window functions.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, SUM(sal) OVER (ORDER BY id) running FROM e ORDER BY id;Result: running is 100, 300, 450, 600, 650, 720
CUME_DIST#
CUME_DIST() OVER (...)Returns the share of the rows of the partition that are before or equal to the current row in the window order (peers included), a number from above 0 to 1.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, CUME_DIST() OVER (ORDER BY sal) cd FROM e ORDER BY id;Result: about 0.5, 1, 0.8333, 0.8333, 0.1667, 0.3333 for id 1 to 6
DENSE_RANK#
DENSE_RANK() OVER (...)Returns the rank of the current row among the peers of the partition, without gaps.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, DENSE_RANK() OVER (PARTITION BY dept ORDER BY sal DESC) dr FROM e ORDER BY id;Result: 2, 1, 1, 1, 2, 1 for id 1 to 6
FIRST_VALUE#
FIRST_VALUE(expr) OVER (...)Returns expr of the first row of the window frame.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, FIRST_VALUE(name) OVER (PARTITION BY dept ORDER BY sal) fv FROM e ORDER BY id;Result: ann, ann, ed, ed, ed, fay for id 1 to 6 (the name with the lowest salary of the department)
LAG#
LAG(expr [, offset [, default]]) OVER (...)Returns expr of the row offset rows (1 by default) before the current row in the partition, or default (NULL by default) if there is no such row.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, LAG(sal) OVER (ORDER BY id) prev FROM e ORDER BY id;Result: NULL, 100, 200, 150, 150, 50 for id 1 to 6
LAST_VALUE#
LAST_VALUE(expr) OVER (...)Returns expr of the last row of the window frame. With the default frame (up to the current row) that is the current row's peer group; write ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to get the last row of the partition. (LAST_VALUE(expr) with one argument and no OVER returns expr.)
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, LAST_VALUE(name) OVER (PARTITION BY dept ORDER BY sal ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) lv FROM e ORDER BY id;Result: bob for id 1 and 2, fay for id 6, and for id 3 to 5 the name of the last row of department 2 (cy and di tie on the salary, so which of them is last is not defined)
LEAD#
LEAD(expr [, offset [, default]]) OVER (...)Returns expr of the row offset rows (1 by default) after the current row in the partition, or default if there is no such row.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, LEAD(sal, 2, -1) OVER (ORDER BY id) nxt FROM e ORDER BY id;Result: 150, 150, 50, 70, -1, -1 for id 1 to 6
NTH_VALUE#
NTH_VALUE(expr, n) OVER (...)Returns expr of the n-th row of the window frame, or NULL if the frame has fewer rows.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, NTH_VALUE(sal, 2) OVER (ORDER BY id) n2 FROM e ORDER BY id;Result: NULL, 200, 200, 200, 200, 200 for id 1 to 6
NTILE#
NTILE(n) OVER (...)Divides the partition into n groups as equal as possible and returns the number of the group of the current row.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, NTILE(3) OVER (ORDER BY id) t FROM e ORDER BY id;Result: 1, 1, 2, 2, 3, 3 for id 1 to 6
PERCENT_RANK#
PERCENT_RANK() OVER (...)Returns (rank - 1) / (rows in the partition - 1), a number from 0 to 1.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, PERCENT_RANK() OVER (ORDER BY sal) pr FROM e ORDER BY id;Result: 0 for the lowest salary (id 5), 1 for the highest (id 2)
RANK#
RANK() OVER (...)Returns the rank of the current row in the partition; peers (rows equal in the window order) have the same rank and the next rank is skipped.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, dept, sal, RANK() OVER (PARTITION BY dept ORDER BY sal DESC) rk FROM e ORDER BY id;Result: rk is 2, 1, 1, 1, 3, 1 for id 1 to 6
ROW_NUMBER#
ROW_NUMBER() OVER (...)Returns the number of the current row in its partition, starting at 1, in the window order. Peers get different numbers; their order is not defined unless the ORDER BY decides it.
Example:
-- table e (id, dept, name, sal): (1,1,'ann',100) (2,1,'bob',200) (3,2,'cy',150) (4,2,'di',150) (5,2,'ed',50) (6,NULL,'fay',70)
SELECT id, ROW_NUMBER() OVER (ORDER BY id) rn FROM e ORDER BY id;Result: rn is 1, 2, 3, 4, 5, 6