Window Function Cheat Sheet
Window Function Cheat Sheet
Default Partition: With no PARTITION BY clause, the entire Default ORDER BY: With no ORDER BY clause, the order of rows
SYNTAX result set is the partition. within each partition is arbitrary.
LOGICAL ORDER OF OPERATIONS IN SQL 1 row before the current row and 1 row after the values in the range between 3 and 5 ORDER BY must 1 group before the current row and 1 group after the
current row contain a single expression current row regardless of the value
* *
Oslo 10 7 6 4 without this row 50% of values are less than this 80% of values are less than or equal to this one
Distribution Functions row's value
percent_rank() ORDER BY and Window Frame: rank() and dense_rank() require ORDER BY, but row_number() does ORDER BY and Window Frame: Distribution functions require ORDER BY. They do not accept window frame
cume_dist() not require ORDER BY. Ranking functions do not accept window frame definition (ROWS, RANGE, GROUPS). definition (ROWS, RANGE, GROUPS).
order by month
1 500 300 1 500 NULL AND UNBOUNDED FOLLOWING)
2 300 400 2 300 500
city month sold first_value city month sold last_value
3 400 100 3 400 300
4 100 500 4 100 400
Paris 1 500 500 Paris 1 500 400
5 500 NULL 5 500 100 Paris 2 300 500 Paris 2 300 400
Paris 3 400 500 Paris 3 400 400
Rome 2 200 200 Rome 2 200 500
lead(sold, 2, 0) OVER(ORDER BY month) lag(sold, 2, 0) OVER(ORDER BY month)
AGGREGATE FUNCTIONS month sold lead month sold lag
Rome 3 300 200 Rome 3 300 500
order by month
order by month
offset = 2
avg(expr) – average value for rows 2 300 100 2 300 0
within the window frame 3 400 500 3 400 500
offset = 2
Note: You usually want to use RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
count(expr) – count of values for 4 100 0 4 100 300
with last_value(). With the default window frame for ORDER BY, RANGE UNBOUNDED PRECEDING,
rows within the window frame 5 500 0 5 500 400
last_value() returns the value for the current row.
max(expr) – maximum value within
the window frame ntile(n) – divide rows within a partition as equally as possible into n groups, and assign each row its nth_value(expr, n) – the value for the n-th row within the window frame; n must be an integer
min(expr) – minimum value within group number.
the window frame ntile(3) ORDER BY and Window Frame: ntile(), lead(), city month sold first_value ORDER BY and Window Frame: first_value(),
city sold ntile
sum(expr) – sum of values within the and lag() require an ORDER BY. They do not accept Paris 1 500 300 last_value(), and nth_value() do not require
Rome 100 1
window frame window frame definition (ROWS, RANGE, GROUPS). Paris 2 300 300 an ORDER BY. They accept window frame definition
Paris 100 1 1
Paris 3 400 300 (ROWS, RANGE, GROUPS).
London 200 1
Rome 2 200 300
Moscow 200 2
ORDER BY and Window Frame: Aggregate Berlin 200 2 2 Rome 3 300 300
functions do not require an ORDER BY. Madrid 300 2 Rome 4 500 300
They accept window frame definition Oslo 300 3 Rome 5 300 300
3
(ROWS, RANGE, GROUPS). Dublin 300 3 London 1 100 NULL