Window Functions Cheat Sheet Mobile
Window Functions Cheat Sheet Mobile
Cheat Sheet
CONTENTS
WINDOW FUNCTIONS 2
AGGREGATE FUNCTIONS VS. WINDOW FUNCTIONS 2
SYNTAX 3
NAMED WINDOW DEFINITION 4
LOGICAL ORDER OF OPERATIONS IN SQL 5
PARTITION BY 6
ORDER BY 7
WINDOW FRAME 8
ABBREVIATIONS 10
DEFAULT WINDOW FRAME 10
LIST OF WINDOW FUNCTIONS 11
AGGREGATE FUNCTIONS 12
RANKING FUNCTIONS 13
DISTRIBUTION FUNCTIONS 14
ANALYTIC FUNCTIONS 15
WINDOW FUNCTIONS
Window functions compute their result based on a sliding window frame,
a set of rows that are somehow related to the current row.
current row
SYNTAX
PARTITION BY, ORDER BY, and window frame definition are all
optional.
1. FROM, JOIN
2. WHERE
3. GROUP BY
4. aggregate functions
5. HAVING
6. window functions
7. SELECT
8. DISTINCT
9. UNION/INTERSECT/EXCEPT
10. ORDER BY
11. OFFSET
12. LIMIT/FETCH/TOP
You can use window functions in SELECT and ORDER BY. However, you
can't put window functions anywhere in the FROM, WHERE, GROUP BY, or
HAVING clauses.
PARTITION BY
divides rows into multiple groups, called partitions, to which the window
function is applied.
PARTITION BY city
month city sold sum
1 Paris 300 800
2 Paris 500 800
1 Rome 200 900
2 Rome 300 900
3 Rome 400 900
1 London 100 500
2 London 400 500
ORDER BY
ORDER BY specifies the order of rows in each partition to which the
window function is applied.
PARTITION BY city
ORDER BY month
sold city month
300 Paris 1
500 Paris 2
200 Rome 1
300 Rome 2
400 Rome 3
100 London 1
400 London 2
Default ORDER BY: With no ORDER BY clause, the order of rows within
each partition is arbitrary.
WINDOW FRAME
A window frame is a set of rows that are somehow related to the current
row. The window frame is evaluated separately within each partition.
UNBOUNDED
N PRECEDING PRECEDING
N ROWS
CURRENT ROW
M ROWS
M FOLLOWING UNBOUNDED
FOLLOWING
UNBOUNDED PRECEDING
n PRECEDING
CURRENT ROW
n FOLLOWING
UNBOUNDED FOLLOWING
1 row before the current row and 1 row after the current row
values in the range between 3 and 5 ORDER BY must contain a single expression
1 group before the current row and 1 group after the current row regardless of the value
ABBREVIATIONS
ABBREVIATION MEANING
UNBOUNDED PRECEDING BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
n PRECEDING BETWEEN n PRECEDING AND CURRENT ROW
CURRENT ROW BETWEEN CURRENT ROW AND CURRENT ROW
n FOLLOWING BETWEEN CURRENT ROW AND n FOLLOWING
UNBOUNDED FOLLOWING BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
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()
first_value()
last_value()
nth_value()
AGGREGATE FUNCTIONS
RANKING FUNCTIONS
row_number() – unique number for each row within partition, with
different 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
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
ANALYTIC FUNCTIONS
lead(expr, offset, default) – the value for the row offset
rows after the current; offset and default are optional; default values:
offset = 1, default = NULL
1 500 300
2 300 400
3 400 100
4 100 500
5 500 NULL
1 500 400
2 300 100
3 400 500
offset = 2
4 100 0
5 500 0
1 500 NULL
2 300 500
3 400 300
4 100 400
5 500 100
1 500 0
offset = 2
2 300 0
3 400 500
4 100 300
5 500 400
ntile(3)
city sold ntile
Rome 100 1
Paris 100 1 1
London 200 1
Moscow 200 2
Berlin 200 2 2
Madrid 300 2
Oslo 300 3
3
Dublin 300 3
first_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
first_value(sold) OVER
(PARTITION BY city ORDER BY month)
last_value(sold) OVER
(PARTITION BY city ORDER BY month
RANGE BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING)
city month sold last_value
Paris 1 500 400
Paris 2 300 400
Paris 3 400 400
Rome 2 200 500
Rome 3 300 500
Rome 4 500 500