![](https://oss-emcsprod-public.modb.pro/pdf/00f2a99f-fc47-4ea8-83d7-c7c59a1fe43b/bg2.jpg)
SQL Window Functions Cheat Sheet
Try out the interactive Window Functions course at LearnSQL.com, and check out our other SQL courses.
LearnSQL.com is owned by Vertabelo SA
vertabelo.com | CC BY-NC-ND Vertabelo SA
city price
row_number rank dense_rank
over(order by price)
Paris 7 1 1 1
Rome 7 2 1 1
London 8.5 3 3 2
Berlin 8.5 4 3 2
Moscow 9 5 5 3
Madrid 10 6 6 4
Oslo 10 7 6 4
LIST OF WINDOW
FUNCTIONS
AGGREGATE FUNCTIONS
RANKING FUNCTIONS
∙ row_number() − unique number for each row within partition, with dierent
numbers for tied values
∙ rank() − ranking within partition, with gaps and same ranking for tied values
∙ dense_rank() − ranking within partition, with no gaps and same ranking for tied values
ANALYTIC FUNCTIONS
∙ lead(expr, oset, default) − the value for the row oset rows aer the current; oset
and default are optional; default values: oset = 1, default = NULL
∙ lag(expr, oset, default) − the value for the row oset rows before the current; oset
and default are optional; default values: oset = 1, default = NULL
∙ nth_value(expr, n) − the value for the n-th row within the window frame; n must be an integer ∙ ntile(n) − divide rows within a partition as equally as possible into n groups, and assign
each row its group number.
∙ rst_value(expr) − the value for the first row within the window frame
∙ last_value(expr) − the value for the last row within the window frame
DISTRIBUTION FUNCTIONS
∙ percent_rank() − the percentile ranking number of a row—a value in [0, 1] interval:
(rank - 1) / (total number of rows - 1)
∙ cume_dist() − the cumulative distribution of a value within a group of values, i.e., the number of
rows with values less than or equal to the current row’s value divided by the total number of rows;
a value in (0, 1] interval
ORDER BY and Window Frame: rank() and dense_rank() require ORDER BY, but
row_number() does not require ORDER BY. Ranking functions do not accept window
frame definition (RO WS, R ANGE, GROUPS).
ORDER BY and Window Frame: rst_value(),
last_value(), and nth_value() do not
require an ORDER BY. They accept window frame
definition (ROWS, RANGE, GROUPS).
ORDER BY and Window Frame: ntile(),
l e a d(), and lag() require an ORDER BY.
They do not accept window frame definition
(ROWS, R ANGE, GROUPS).
ORDER BY and Window Frame:
Aggregate functions do not
require an ORDER BY. They accept
window frame definition (ROWS,
RANGE, GROUPS).
Note: You usually want to use RANGE BETWEEN
UNBOUNDED PRECEDING AND UNBOUNDED
FOLLOWING with last_value(). With the default
window frame for ORDER BY, RANGE UNBOUNDED
PRECEDING, last_value() returns the value for
the current row.
ORDER BY and Window Frame: Distribution functions require ORDER BY. They do not accept window frame
definition (ROWS, RANGE, GROUPS).
Aggregate Functions
∙ avg()
∙ count()
∙ max()
∙ min()
∙ sum()
Ranking Functions
∙ row_number()
∙ rank()
∙ dense_rank()
Distribution Functions
∙ percent_rank()
∙ cume_dist()
Analytic Functions
∙ lead()
∙ lag()
∙ ntile()
∙ rst_value()
∙ last_value()
∙ nth_value()
∙ avg(expr) − average value for
rows within the window frame
∙ count(expr) − count of values
for rows within the window
frame
∙ m a x (expr) − maximum value
within the window frame
∙ min(expr) − minimum value
within the window frame
∙ sum(expr) − sum of values
within the window frame
month sold
1 500
2 300
3 400
4 100
5 500
NULL
500
300
400
100
lag(sold) OVER(ORDER BY month)
order by month
city month sold
Paris 1 500
Paris 2 300
Paris 3 400
Rome 2 200
Rome 3 300
Rome 4 500
first_value
500
500
500
200
200
200
rst_value(sold) OVER
(PARTITION BY city ORDER BY month)
city month sold
Paris 1 500
Paris 2 300
Paris 3 400
Rome 2 200
Rome 3 300
Rome 4 500
last_value
400
400
400
500
500
500
last_value(sold) OVER
(PARTITION BY city ORDER BY month
RANGE BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING)
month sold
1 500
2 300
3 400
4 100
5 500
300
400
100
500
NULL
lead(sold) OVER(ORDER BY month)
order by month
city sold cume_dist
Paris 100 0.2
Berlin 150 0.4
Rome 200 0.8
Moscow 200 0.8
London 300 1
80% of values are
less than or equal
to this one
cume_dist() OVER(ORDER BY sold)
city sold percent_rank
Paris 100 0
Berlin 150 0.25
Rome 200 0.5
Moscow 200 0.5
London 300 1
without this row 50% of
values are less than this
row’s value
percent_rank() OVER(ORDER BY sold)
city month sold
Paris 1 500
Paris 2 300
Paris 3 400
Rome 2 200
Rome 3 300
Rome 4 500
Rome 5 300
London 1 100
nth_value
300
300
300
300
300
300
300
NULL
nth_value(sold, 2) OVER
(PARTITION BY city ORDER BY month)
month sold
1 500
2 300
3 400
4 100
5 500
0
0
500
300
400
lag(sold, 2, 0) OVER(ORDER BY month)
order by month
oset=2
month sold
1 500
2 300
3 400
4 100
5 500
400
100
500
0
0
lead(sold, 2, 0) OVER(ORDER BY month)
order by month
oset=2
1
1
1
2
2
2
3
3
3
city sold
Rome 100
Paris 100
London 200
Moscow 200
Berlin 200
Madrid 300
Oslo 300
Dublin 300
1
1
1
2
2
2
3
3
ntile(3)
1
2
3
评论