0% found this document useful (0 votes)
19 views1 page

Window Functions Cheat Sheet A3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views1 page

Window Functions Cheat Sheet A3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

SQL Window Functions Cheat Sheet

WINDOW FUNCTIONS AGGREGATE FUNCTIONS VS. WINDOW FUNCTIONS PARTITION BY ORDER BY


Window functions compute their result based on a Unlike aggregate functions, window functions do not collapse rows. divides rows into multiple groups, called partitions, to which ORDER BY specifies the order of rows in each partition to which the
sliding window frame, a set of rows that are the window function is applied. window function is applied.
somehow related to the current row.
Aggregate Functions Window Functions PARTITION BY city PARTITION BY city
ORDER BY month
month city sold month city sold sum
∑ 1 Rome 200 1 Paris 300 800
sold city month sold city month
200 Rome 1 300 Paris 1
2 Paris 500 2 Paris 500 800
current row ∑ 500 Paris 2 500 Paris 2
1 London 100 1 Rome 200 900
100 London 1 200 Rome 1
∑ ∑ 1 Paris 300 2 Rome 300 900 300 Paris 1 300 Rome 2
2 Rome 300 3 Rome 400 900 300 Rome 2 400 Rome 3
2 London 400 1 London 100 500 400 London 2 100 London 1
3 Rome 400 2 London 400 500 400 Rome 3 400 London 2

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

LOGICAL ORDER OF OPERATIONS IN SQL London


Rome
100
300
6
6
London
Rome
100
300
6
6
London
Rome
100
300
6
6

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.

LIST OF WINDOW FUNCTIONS RANKING FUNCTIONS DISTRIBUTION FUNCTIONS


row_number() – unique number for each row within partition, with different numbers for tied values percent_rank() – the percentile ranking number of a row—a value in [0, 1] interval:
rank() – ranking within partition, with gaps and same ranking for tied values (rank-1) / (total number of rows - 1)
Aggregate Functions dense_rank() – ranking within partition, with no gaps and same ranking for tied values cume_dist() – the cumulative distribution of a value within a group of values, i.e., the number of rows
avg() with values less than or equal to the current row’s value divided by the total number of rows; a value in (0,
count() 1] interval
max()
row_number rank dense_rank
city price
min() over(order by price) percent_rank() OVER(ORDER BY sold) cume_dist() OVER(ORDER BY sold)
sum() Paris 7 1 1 1 city sold percent_rank city sold cume_dist
Rome 7 2 1 1 Paris 100 0 Paris 100 0.2
London 8.5 3 3 2 Berlin 150 0.25 Berlin 150 0.4
Ranking Functions
row_number()
rank()
Berlin
Moscow
8.5
9
4
5
3
5
2
3
Rome
Moscow
London
200
200
300
0.5
0.5
1
* Rome
Moscow
London
200
200
300
0.8
0.8
1
*
Madrid 10 6 6 4
dense_rank()

* *
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

1 500 300 1 500 NULL


city month sold first_value city month sold last_value
2 300 400 2 300 500
3 400 100 3 400 300
Paris 1 500 500 Paris 1 500 400
4 100 500 4 100 400 Paris 2 300 500 Paris 2 300 400
5 500 NULL 5 500 100 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) Rome 3 300 200 Rome 3 300 500
Rome 4 500 200 Rome 4 500 500
AGGREGATE FUNCTIONS month sold lead month sold lag
order by month

order by month

1 500 400 1 500 0


offset = 2

2 300 100 2 300 0


3 400 500 3 400 500
offset = 2

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

LearnSQL.com is owned by Vertabelo SA


Try out the interactive Window Functions course or explore all LearnSQL.com courses. vertabelo.com | CC BY-NC-ND Vertabelo SA

You might also like