Window Functions Cheat Sheet A3
Window Functions Cheat Sheet A3
SYNTAX
Default Partition: With no PARTITION BY clause, the entire Default ORDER BY: With no ORDER BY clause, the order of rows
result set is the partition. within each partition is arbitrary.
SELECT city, month, SELECT <column_1>, <column_2>,
SUM(sold) OVER ( <window_function> OVER (
PARTITION BY city PARTITION BY <...> WINDOW FRAME
ORDER BY month ORDER BY <...>
A window frame is a set of rows that are somehow related to the current row. The window frame is evaluated separately within each
RANGE UNBOUNDED PRECEDING) total <window_frame>) <window_column_alias>
partition.
FROM sales; FROM <table_name>;
<ROWS | RANGE | GROUPS> BETWEEN lower_bound AND upper_bound
NAMED WINDOW DEFINITION UNBOUNDED The bounds can be any of the five options:
N PRECEDING PRECEDING
UNBOUNDED PRECEDING
SELECT country, city, SELECT <column_1>, <column_2>, N ROWS
n PRECEDING
RANK() OVER country_sold_avg <window_function>() OVER <window_name> CURRENT ROW CURRENT ROW
FROM sales FROM <table_name>
M ROWS n FOLLOWING
WHERE month BETWEEN 1 AND 6 WHERE <...>
UNBOUNDED FOLLOWING
GROUP BY country, city GROUP BY <...> M FOLLOWING UNBOUNDED
HAVING sum(sold) > 10000 HAVING <...> FOLLOWING
The lower_bound must be BEFORE the upper_bound.
WINDOW country_sold_avg AS ( WINDOW <window_name> AS (
PARTITION BY country PARTITION BY <...> ROWS BETWEEN 1 PRECEDING RANGE BETWEEN 1 PRECEDING GROUPS BETWEEN 1 PRECEDING
ORDER BY avg(sold) DESC) ORDER BY <...> AND 1 FOLLOWING AND 1 FOLLOWING AND 1 FOLLOWING
ORDER BY country, city; <window_frame>) city sold month city sold month city sold month
Paris 300 1 Paris 300 1 Paris 300 1
ORDER BY <...>; Rome 200 1 Rome 200 1 Rome 200 1
Paris 500 2 Paris 500 2 Paris 500 2
Rome 100 4 Rome 100 4 Rome 100 4
current current current
PARTITION BY, ORDER BY, and window frame definition are all optional. row Paris 200 4 row Paris 200 4 row Paris 200 4
Paris 300 5 Paris 300 5 Paris 300 5
Rome 200 5 Rome 200 5 Rome 200 5
London 200 5 London 200 5 London 200 5
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
1. FROM, JOIN 7. SELECT As of 2024, GROUPS is only supported in PostgreSQL 11 and up.
2. WHERE 8. DISTINCT
3. GROUP BY 9. UNION/INTERSECT/EXCEPT
4. aggregate functions 10. ORDER BY ABBREVIATIONS DEFAULT WINDOW FRAME
5. HAVING 11. OFFSET ABBREVIATION MEANING If ORDER BY is specified, then the frame is
6. window functions 12. LIMIT/FETCH/TOP UNBOUNDED PRECEDING BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW RANGE BETWEEN UNBOUNDED PRECEDING
n PRECEDING BETWEEN n PRECEDING AND CURRENT ROW AND CURRENT ROW.
CURRENT ROW BETWEEN CURRENT ROW AND CURRENT ROW
Without ORDER BY, the frame specification is
You can use window functions in SELECT and ORDER BY. However, you can't put window functions anywhere in the FROM, WHERE, n FOLLOWING BETWEEN CURRENT ROW AND n FOLLOWING ROWS BETWEEN UNBOUNDED PRECEDING
GROUP BY, or HAVING clauses. UNBOUNDED FOLLOWING BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING AND UNBOUNDED FOLLOWING.
* *
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
row's value
Distribution Functions
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
percent_rank()
not require ORDER BY. Ranking functions do not accept window frame definition (ROWS, RANGE, GROUPS). definition (ROWS, RANGE, GROUPS).
cume_dist()
ANALYTIC FUNCTIONS
Analytic Functions lead(expr, offset, default) – the value for the row offset rows after the current; offset and default first_value(expr) – the value for the first row within the window frame
lead() are optional; default values: offset = 1, default = NULL last_value(expr) – the value for the last row within the window frame
lag() lag(expr, offset, default) – the value for the row offset rows before the current; offset and default
ntile() are optional; default values: offset = 1, default = NULL
first_value()
first_value(sold) OVER last_value(sold) OVER
last_value()
(PARTITION BY city ORDER BY month) (PARTITION BY city ORDER BY month
nth_value() lead(sold) OVER(ORDER BY month) lag(sold) OVER(ORDER BY month)
RANGE BETWEEN UNBOUNDED PRECEDING
month sold lead month sold lag AND UNBOUNDED FOLLOWING)
order by month
order by month
order by month
avg(expr) – average value for rows Note: You usually want to use RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
4 100 0 4 100 300
within the window frame with last_value(). With the default window frame for ORDER BY, RANGE UNBOUNDED PRECEDING,
5 500 0 5 500 400 last_value() returns the value for the current row.
count(expr) – count of values for
rows within the window frame
max(expr) – maximum value within 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
the window frame group number.
min(expr) – minimum value within
the window frame
ntile(3) ORDER BY and Window Frame: ntile(), lead(), city month sold nth_value ORDER BY and Window Frame: first_value(),
sum(expr) – sum of values within the city sold ntile
window frame 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 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