Ultimate SQL Cheat Sheet 1680810988
Ultimate SQL Cheat Sheet 1680810988
Default Partition: with no PARTITION BY clause, the entire Default ORDER BY: with no ORDER BY clause, the order of
SYNTAX result set is the partition. rows within each partition is arbitrary.
1. FROM, JOIN 7. SELECT As of 2020, 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
6. window functions 12. LIMIT/FETCH/TOP Abbreviation Meaning If ORDER BY is specified, then the frame is
UNBOUNDED PRECEDING BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW RANGE BETWEEN UNBOUNDED PRECEDING AND
n PRECEDING BETWEEN n PRECEDING AND CURRENT ROW CURRENT ROW.
You can use window functions in SELECT and ORDER BY. However, you can’t put window functions anywhere in the FROM, CURRENT ROW BETWEEN CURRENT ROW AND CURRENT ROW Without ORDER BY, the frame specification is
WHERE, GROUP BY, or HAVING clauses. n FOLLOWING BETWEEN AND CURRENT ROW AND n FOLLOWING ROWS BETWEEN UNBOUNDED PRECEDING AND
UNBOUNDED FOLLOWING BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING UNBOUNDED FOLLOWING.
Distribution Functions ORDER BY and Window Frame: rank() and dense_rank() require ORDER BY, but ORDER BY and Window Frame: Distribution functions require ORDER BY. They do not accept window frame
∙ percent_rank() row_number() does not require ORDER BY. Ranking functions do not accept window definition (ROWS, RANGE, GROUPS).
frame definition (ROWS, RANGE, GROUPS).
∙ cume_dist()
Analytic Functions
∙ lead()
ANALYTIC FUNCTIONS ∙
first_value(expr) − the value for the first row within the window frame
∙
lead(expr, offset, default) − the value for the row offset rows after the current; offset and ∙
last_value(expr) − the value for the last row within the window frame
∙ lag()
default are optional; default values: offset = 1, default = NULL
∙ntile()
∙
lag(expr, offset, default) − the value for the row offset rows before the current; offset and first_value(sold) OVER last_value(sold) OVER
∙ first_value() (PARTITION BY city ORDER BY month) (PARTITION BY city ORDER BY month
default are optional; default values: offset = 1, default = NULL
∙ last_value() RANGE BETWEEN UNBOUNDED PRECEDING
lead(sold) OVER(ORDER BY month) lag(sold) OVER(ORDER BY month) city month sold first_value AND UNBOUNDED FOLLOWING)
∙ nth_value() Paris 1 500 500
month sold month sold city month sold last_value
order by month
order by month
Paris 2 300 500
1 500 300 1 500 NULL Paris 3 400 500 Paris 1 500 400
2 300 400 2 300 500 Rome 2 200 200 Paris 2 300 400
Paris 3 400 400
AGGREGATE FUNCTIONS 3 400 100 3 400 300 Rome 3 300 200
Rome 2 200 500
4 100 500 4 100 400 Rome 4 500 200
5 500 NULL 5 500 100 Rome 3 300 500
∙
avg(expr) − average value for Rome 4 500 500
rows within the window frame
lead(sold, 2, 0) OVER(ORDER BY month) lag(sold, 2, 0) OVER(ORDER BY month)
Note: You usually want to use RANGE BETWEEN
month sold month sold
order by month
order by month
∙
count(expr) − count of values UNBOUNDED PRECEDING AND UNBOUNDED
offset=2
1 500 400 1 500 0
for rows within the window FOLLOWING with last_value(). With the default
2 300 100 2 300 0
frame 3 400 500 3 400 500 window frame for ORDER BY, RANGE UNBOUNDED
offset=2
∙
ntile(n) − divide rows within a partition as equally as possible into n groups, and assign each ∙
nth_value(expr, n) − the value for the n-th row within the window frame; n must be an integer
∙
min(expr) − minimum value
row its group number. nth_value(sold, 2) OVER (PARTITION BY city
within the window frame ORDER BY month RANGE BETWEEN UNBOUNDED
ntile(3) PRECEDING AND UNBOUNDED FOLLOWING)
∙
sum(expr) − sum of values within city sold city month sold nth_value
the window frame Rome 100 1 Paris 1 500 300
Paris 100 1 1 1 Paris 2 300 300
London 200 1 Paris 3 400 300
Moscow 200 2 Rome 2 200 300
ORDER BY and Window Frame: Berlin 200 2 2 2
ORDER BY and Window Frame: ntile(), ORDER BY and Window Frame: first_value(),
Rome 3 300 300
Aggregate functions do not require an Madrid 300 2 lead(), and lag() require an ORDER BY. Rome 4 500 300
last_value(), and nth_value() do not
ORDER BY. They accept window frame Oslo 300 3 They do not accept window frame definition Rome 5 300 300 require an ORDER BY. They accept window frame
3 3
definition (ROWS, RANGE, GROUPS). Dublin 300 3 (ROWS, RANGE, GROUPS). London 1 100 NULL definition (ROWS, RANGE, GROUPS).
JOIN typically combines rows with equal values for the specified columns. Usually, one table contains a primary key,
which is a column or columns that uniquely identify rows in the table (the cat_id column in the cat table).
The other table has a column or columns that refer to the primary key columns in the first table (the cat_id column in RIGHT JOIN
the toy table). Such columns are foreign keys. The JOIN condition is the equality between the primary key columns in
RIGHT JOIN returns all rows from the right table with matching rows from the left table. Rows without a match are
one table and columns referring to them in the other table.
filled with NULLs. RIGHT JOIN is also called RIGHT OUTER JOIN.
SELECT *
JOIN FROM toy
toy_id
5
toy_name
ball
cat_id
1
cat_id
1
cat_name
Kitty
JOIN returns all rows that match the ON condition. JOIN is also called INNER JOIN. RIGHT JOIN cat 3 mouse 1 1 Kitty
ON toy.cat_id = cat.cat_id; NULL NULL NULL 2 Hugo
SELECT * toy_id toy_name cat_id cat_id cat_name 1 ball 3 3 Sam
FROM toy 5 ball 1 1 Kitty 4 mouse 4 4 Misty
JOIN cat 3 mouse 1 1 Kitty whole right table
ON toy.cat_id = cat.cat_id; 1 ball 3 3 Sam
4 mouse 4 4 Misty
There is also another, older syntax, but it isn't recommended.
List joined tables in the FROM clause, and place the conditions in the WHERE clause. FULL JOIN
SELECT * FULL JOIN returns all rows from the left table and all rows from the right table. It fills the non-matching rows with
FROM toy, cat NULLs. FULL JOIN is also called FULL OUTER JOIN.
WHERE toy.cat_id = cat.cat_id; SELECT * toy_id toy_name cat_id cat_id cat_name
FROM toy 5 ball 1 1 Kitty
JOIN CONDITIONS FULL JOIN cat 3
NULL
mouse
NULL
1
NULL
1
2
Kitty
Hugo
ON toy.cat_id = cat.cat_id;
The JOIN condition doesn't have to be an equality – it can be any condition you want. JOIN doesn't interpret the JOIN 1 ball 3 3 Sam
condition, it only checks if the rows satisfy the given condition. 4 mouse 4 4 Misty
2 spring NULL NULL NULL
whole left table whole right table
To refer to a column in the JOIN query, you have to use the full column name: first the table name, then a dot (.) and the
column name:
ON cat.cat_id = toy.cat_id
You can omit the table name and use just the column name if the name of the column is unique within all columns in the CROSS JOIN
joined tables.
CROSS JOIN returns all possible combinations of rows from the left and right tables.
SELECT *
NATURAL JOIN FROM toy
toy_id
1
toy_name
ball
cat_id
3
cat_id
1
cat_name
Kitty
If the tables have columns with the same name, you can use CROSS JOIN cat; 2 spring NULL 1 Kitty
NATURAL JOIN instead of JOIN. cat_id toy_id toy_name cat_name 3 mouse 1 1 Kitty
1 5 ball Kitty Other syntax: 4 mouse 4 1 Kitty
SELECT * 1 3 mouse Kitty 5 ball 1 1 Kitty
SELECT *
FROM toy 3 1 ball Sam 1 ball 3 2 Hugo
FROM toy, cat;
NATURAL JOIN cat; 4 4 mouse Misty 2 spring NULL 2 Hugo
3 mouse 1 2 Hugo
The common column appears only once in the result table. 4 mouse 4 2 Hugo
Note: NATURAL JOIN is rarely used in real life. 5 ball 1 2 Hugo
1 ball 3 3 Sam
··· ··· ··· ··· ···
CAT AS c OWNER AS o
NON-EQUI SELF JOIN cat_id cat_name mom_id owner_id age id name age
You can use a non-equality in the ON condition, for example, to show all different pairs of rows. 1 Kitty 5 1 17 1 John Smith 18
2 Hugo 1 2 10 2 Danielle Davis 10
TOY AS a TOY AS b 3 Sam 2 2 5
toy_id toy_name cat_id cat_id toy_id toy_name 4 Misty 1 NULL 11
3 mouse 1 1 3 mouse
5 ball 1 1 5 ball
1 ball 3 3 1 ball SELECT
4 mouse 4 4 4 mouse cat_name,
2 spring NULL NULL 2 spring o.name AS owner_name,
c.age AS cat_age, cat_name owner_name age age
SELECT cat_a_id toy_a cat_b_id toy_b o.age AS owner_age Kitty John Smith 17 18
a.toy_name AS toy_a, 1 mouse 3 ball FROM cat c Sam Danielle Davis 5 10
b.toy_name AS toy_b 1 ball 3 ball
JOIN owner o
FROM toy a 1 mouse 4 mouse
1 ball 4 mouse ON c.owner_id = o.id
JOIN toy b
3 ball 4 mouse AND c.age < o.age;
ON a.cat_id < b.cat_id;