0% found this document useful (0 votes)
19 views6 pages

SQL 1690091465

SQL is a language used to query and manipulate databases. It allows you to select, filter, and organize data across single or multiple tables. Some common SQL operations include selecting columns, filtering rows, joining tables, and sorting results.

Uploaded by

Anna Denisova
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 views6 pages

SQL 1690091465

SQL is a language used to query and manipulate databases. It allows you to select, filter, and organize data across single or multiple tables. Some common SQL operations include selecting columns, filtering rows, joining tables, and sorting results.

Uploaded by

Anna Denisova
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/ 6

SQL Basics Cheat Sheet

SQL FILTERING THE OUTPUT QUERYING MULTIPLE TABLES


SQL, or Structured Query Language, is a language to talk COMPARISON OPERATORS INNER JOIN FULL JOIN
to databases. It allows you to select specific data and to
Fetch names of cities that have a rating above 3: JOIN (or explicitly INNER JOIN) returns rows that have FULL JOIN (or explicitly FULL OUTER JOIN) returns all
build complex reports. Today, SQL is a universal language
matching values in both tables. rows from both tables – if there's no matching row in the
of data. It is used in practically all technologies that process SELECT name second table, NULL s are returned.
data. FROM city
WHERE rating > 3; SELECT city.name, country.name SELECT city.name, country.name
SAMPLE DATA FROM city FROM city
[INNER] JOIN country FULL [OUTER] JOIN country
COUNTRY
Fetch names of cities that are neither Berlin nor Madrid: ON city.country_id = country.id; ON city.country_id = country.id;
id name population area
1 France 66600000 640680 SELECT name CITY COUNTRY
2 Germany 80700000 357000 FROM city CITY COUNTRY id name country_id id name
... ... ... ... id name country_id id name 1 Paris 1 1 France
WHERE name != 'Berlin'
1 Paris 1 1 France 2 Berlin 2 2 Germany
CITY AND name != 'Madrid';
2 Berlin 2 2 Germany 3 Warsaw 4 NULL NULL
id name country_id population rating 3 Warsaw 4 3 Iceland NULL NULL NULL 3 Iceland
1 Paris 1 2243000 5
2 Berlin 2 3460000 3 TEXT OPERATORS
... ... ... ... ...
Fetch names of cities that start with a 'P' or end with an 's':
QUERYING SINGLE TABLE SELECT name
LEFT JOIN CROSS JOIN
FROM city
Fetch all columns from the country table: CROSS JOIN returns all possible combinations of rows
WHERE name LIKE 'P%' LEFT JOIN returns all rows from the left table with
SELECT * OR name LIKE '%s'; corresponding rows from the right table. If there's no from both tables. There are two syntaxes available.
FROM country; matching row, NULL s are returned as values from the SELECT city.name, country.name
second table. FROM city
Fetch id and name columns from the city table: Fetch names of cities that start with any letter followed by SELECT city.name, country.name CROSS JOIN country;
SELECT id, name 'ublin' (like Dublin in Ireland or Lublin in Poland): FROM city SELECT city.name, country.name
FROM city; SELECT name LEFT JOIN country FROM city, country;
FROM city ON city.country_id = country.id;
Fetch city names sorted by the rating column CITY COUNTRY
WHERE name LIKE '_ublin'; CITY COUNTRY id name country_id id name
in the default ASCending order:
id name country_id id name 1 Paris 1 1 France
SELECT name 1 Paris 1 1 France 1 Paris 1 2 Germany
FROM city OTHER OPERATORS 2 Berlin 2 2 Germany 2 Berlin 2 1 France
ORDER BY rating [ASC]; 3 Warsaw 4 NULL NULL 2 Berlin 2 2 Germany
Fetch names of cities that have a population between
500K and 5M:
Fetch city names sorted by the rating column
in the DESCending order: SELECT name
FROM city
SELECT name
FROM city
WHERE population BETWEEN 500000 AND RIGHT JOIN NATURAL JOIN
5000000;
ORDER BY rating DESC; RIGHT JOIN returns all rows from the right table with
NATURAL JOIN will join tables by all columns with the
same name.
corresponding rows from the left table. If there's no
ALIASES Fetch names of cities that don't miss a rating value: matching row, NULL s are returned as values from the left
table.
SELECT city.name, country.name
FROM city
COLUMNS SELECT name
NATURAL JOIN country;
FROM city SELECT city.name, country.name
SELECT name AS city_name WHERE rating IS NOT NULL; CITY COUNTRY
FROM city
FROM city; country_id id name name id
RIGHT JOIN country
6 6 San Marino San Marino 6
ON city.country_id = country.id;
TABLES Fetch names of cities that are in countries with IDs 1, 4, 7, 7 7 Vatican City Vatican City 7
5 9 Greece Greece 9
SELECT co.name, ci.name or 8: CITY COUNTRY
10 11 Monaco Monaco 10
id name country_id id name
FROM city AS ci SELECT name NATURAL JOIN used these columns to match rows:
1 Paris 1 1 France
JOIN country AS co FROM city 2 Berlin 2 2 Germany city.id, city.name, country.id, country.name
ON ci.country_id = co.id; WHERE country_id IN (1, 4, 7, 8); NULL NULL NULL 3 Iceland NATURAL JOIN is very rarely used in practice.

LearnSQL.com is owned by Vertabelo SA


Try out the interactive SQL Basics course at LearnSQL.com, and check out our other SQL courses. vertabelo.com | CC BY-NC-ND Vertabelo SA
SQL Basics Cheat Sheet
AGGREGATION AND GROUPING SUBQUERIES SET OPERATIONS
GROUP BY groups together rows that have the same values in specified A subquery is a query that is nested inside another query, or inside another Set operations are used to combine the results of two or more queries into a
columns. subquery. There are different types of subqueries. single result. The combined queries must return the same number of columns
It computes summaries (aggregates) for each unique combination of values. and compatible data types. The names of the corresponding columns can be
different.
SINGLE VALUE
CITY
id name country_id The simplest subquery returns exactly one column and exactly one row. It
CYCLING SKATING
1 Paris 1
CITY
can be used with comparison operators =, <, <=, >, or >=.
101 Marseille 1 id name country id name country
country_id count This query finds cities with the same rating as Paris: 1 YK DE 1 YK DE
102 Lyon 1
1 3 2 ZG DE 2 DF DE
2 Berlin 2 SELECT name FROM city
2 3 3 WT PL 3 AK PL
103 Hamburg 2
4 2
WHERE rating = (
104 Munich 2 ... ... ... ... ... ...
SELECT rating
3 Warsaw 4 FROM city
105 Cracow 4
WHERE name = 'Paris'
UNION
);
AGGREGATE FUNCTIONS UNION combines the results of two result sets and removes duplicates.
•  
avg(expr) − average value for rows within the group UNION ALL doesn't remove duplicate rows.
MULTIPLE VALUES
•  count(expr) − count of values for rows within the group This query displays German cyclists together with German skaters:
A subquery can also return multiple columns or multiple rows. Such
subqueries can be used with operators IN, EXISTS, ALL, or ANY. SELECT name
•  m ax(expr) − maximum value within the group
FROM cycling
•  min(expr) − minimum value within the group This query finds cities in countries that have a population above 20M: WHERE country = 'DE'
•  sum(expr) − sum of values within the group SELECT name UNION / UNION ALL
FROM city SELECT name
EXAMPLE QUERIES WHERE country_id IN ( FROM skating
SELECT country_id WHERE country = 'DE';
Find out the number of cities: FROM country
SELECT COUNT(*) WHERE population > 20000000
FROM city; ); INTERSECT
INTERSECT returns only rows that appear in both result sets.
Find out the number of cities with non-null ratings: CORRELATED This query displays German cyclists who are also German skaters at the same
SELECT COUNT(rating) time:
A correlated subquery refers to the tables introduced in the outer query.
FROM city;
A correlated subquery depends on the outer query. It cannot be run SELECT name
independently from the outer query. FROM cycling
Find out the number of distinctive country values: WHERE country = 'DE'
SELECT COUNT(DISTINCT country_id) This query finds cities with a population greater than the average
INTERSECT
FROM city; population in the country:
SELECT name
SELECT * FROM skating
Find out the smallest and the greatest country populations: FROM city main_city WHERE country = 'DE';
WHERE population > (
SELECT MIN(population), MAX(population)
SELECT AVG(population)
FROM country;
FROM city average_city EXCEPT
WHERE average_city.country_id = main_city.country_id
Find out the total population of cities in respective countries: EXCEPT returns only the rows that appear in the first result set but do not
);
SELECT country_id, SUM(population) appear in the second result set.
FROM city This query finds countries that have at least one city: This query displays German cyclists unless they are also German skaters at
GROUP BY country_id; SELECT name the same time:
FROM country SELECT name
Find out the average rating for cities in respective countries if the average WHERE EXISTS ( FROM cycling
is above 3.0: SELECT * WHERE country = 'DE'
SELECT country_id, AVG(rating) FROM city EXCEPT / MINUS
FROM city WHERE country_id = country.id SELECT name
GROUP BY country_id ); FROM skating
HAVING AVG(rating) > 3.0; WHERE country = 'DE';

LearnSQL.com is owned by Vertabelo SA


Try out the interactive SQL Basics course at LearnSQL.com, and check out our other SQL courses. vertabelo.com | CC BY-NC-ND Vertabelo SA
SQL JOINs Cheat Sheet
JOINING TABLES LEFT JOIN
JOIN combines data from two tables. LEFT JOIN returns all rows from the left table with matching rows from the right table. Rows without a match
are filled with NULLs. LEFT JOIN is also called LEFT OUTER JOIN.
TOY CAT
toy_id toy_name cat_id cat_id cat_name SELECT * toy_id toy_name cat_id cat_id cat_name
1 ball 3 1 Kitty FROM toy 5 ball 1 1 Kitty
2 spring NULL 2 Hugo LEFT JOIN cat 3 mouse 1 1 Kitty
3 mouse 1 3 Sam ON toy.cat_id = cat.cat_id; 1 ball 3 3 Sam
4 mouse 4 4 Misty 4 mouse 4 4 Misty
5 ball 1 2 spring NULL NULL NULL
whole left table

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
RIGHT JOIN
column in the toy table). Such columns are foreign keys. The JOIN condition is the equality between the RIGHT JOIN returns all rows from the right table with matching rows from the left table. Rows without a match
primary key columns in one table and columns referring to them in the other table. are filled with NULLs. RIGHT JOIN is also called RIGHT OUTER JOIN.
SELECT * toy_id toy_name cat_id cat_id cat_name
FROM toy 5 ball 1 1 Kitty
JOIN RIGHT JOIN cat 3 mouse 1 1 Kitty
ON toy.cat_id = cat.cat_id; NULL NULL NULL 2 Hugo
JOIN returns all rows that match the ON condition. JOIN is also called INNER JOIN.
1 ball 3 3 Sam
SELECT * toy_id toy_name cat_id cat_id cat_name 4 mouse 4 4 Misty
FROM toy 5 ball 1 1 Kitty whole right table
JOIN cat 3 mouse 1 1 Kitty
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. FULL JOIN
List joined tables in the FROM clause, and place the conditions in the WHERE clause. FULL JOIN returns all rows from the left table and all rows from the right table. It fills the non-matching rows
SELECT * with NULLs. FULL JOIN is also called FULL OUTER JOIN.
FROM toy, cat SELECT * toy_id toy_name cat_id cat_id cat_name
WHERE toy.cat_id = cat.cat_id; FROM toy 5 ball 1 1 Kitty
FULL JOIN cat 3 mouse 1 1 Kitty
ON toy.cat_id = cat.cat_id; NULL NULL NULL 2 Hugo
JOIN CONDITIONS 1 ball 3 3 Sam
4 mouse 4 4 Misty
The JOIN condition doesn't have to be an equality – it can be any condition you want. JOIN doesn't interpret 2 spring NULL NULL NULL
the JOIN condition, it only checks if the rows satisfy the given condition. 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 CROSS JOIN
You can omit the table name and use just the column name if the name of the column is unique within all CROSS JOIN returns all possible combinations of rows from the left and right tables.
columns in the joined tables.
SELECT * toy_id toy_name cat_id cat_id cat_name
FROM toy 1 ball 3 1 Kitty
NATURAL JOIN CROSS JOIN cat; 2
3
spring
mouse
NULL
1
1
1
Kitty
Kitty
If the tables have columns with the same name, you can use Other syntax: 4 mouse 4 1 Kitty
NATURAL JOIN instead of JOIN. cat_id toy_id toy_name cat_name SELECT * 5 ball 1 1 Kitty
1 5 ball Kitty FROM toy, cat; 1 ball 3 2 Hugo
SELECT * 1 3 mouse Kitty 2 spring NULL 2 Hugo
FROM toy 3 1 ball Sam 3 mouse 1 2 Hugo
NATURAL JOIN cat; 4 4 mouse Misty 4 mouse 4 2 Hugo
5 ball 1 2 Hugo
The common column appears only once in the result table. 1 ball 3 3 Sam
Note: NATURAL JOIN is rarely used in real life. ··· ··· ··· ··· ···

LearnSQL.com is owned by Vertabelo SA


Try out the interactive SQL JOINs course at LearnSQL.com, and check out our other SQL courses. vertabelo.com | CC BY-NC-ND Vertabelo SA
SQL JOINs Cheat Sheet
COLUMN AND TABLE ALIASES MULTIPLE JOINS
Aliases give a temporary name to a table or a column in a table. You can join more than two tables together. First, two tables are joined, then the third table is joined to the
result of the previous joining.
CAT AS c OWNER AS o
cat_id cat_name mom_id owner_id id name
1 Kitty 5 1 1 John Smith TOY AS t
CAT AS c
2 Hugo 1 2 2 Danielle Davis toy_id toy_name cat_id OWNER AS o
cat_id cat_name mom_id owner_id
3 Sam 2 2 1 ball 3 id name
1 Kitty 5 1
4 Misty 1 NULL 2 spring NULL 1
John
2 Hugo 1 2 Smith
3 mouse 1
A column alias renames a column in the result. A table alias renames a table within the query. If you define a 3 Sam 2 2 2
Danielle
4 mouse 4 Davis
4 Misty 1 NULL
table alias, you must use it instead of the table name everywhere in the query. The AS keyword is optional in 5 ball 1
defining aliases.
SELECT JOIN & JOIN JOIN & LEFT JOIN LEFT JOIN & LEFT JOIN
o.name AS owner_name, cat_name owner_name SELECT SELECT SELECT
c.cat_name Kitty John Smith t.toy_name, t.toy_name, t.toy_name,
FROM cat AS c Sam Danielle Davis c.cat_name, c.cat_name, c.cat_name,
JOIN owner AS o Hugo Danielle Davis
o.name AS owner_name o.name AS owner_name o.name AS owner_name
ON c.owner_id = o.id; FROM toy t FROM toy t FROM toy t
JOIN cat c JOIN cat c LEFT JOIN cat c
SELF JOIN ON t.cat_id = c.cat_id ON t.cat_id = c.cat_id ON t.cat_id = c.cat_id
JOIN owner o LEFT JOIN owner o LEFT JOIN owner o
You can join a table to itself, for example, to show a parent-child relationship. ON c.owner_id = o.id; ON c.owner_id = o.id; ON c.owner_id = o.id;
CAT AS child CAT AS mom toy_name cat_name owner_name toy_name cat_name owner_name toy_name cat_name owner_name
cat_id cat_name owner_id mom_id cat_id cat_name owner_id mom_id John John John
ball Kitty ball Kitty
1 Kitty 1 5 1 Kitty 1 5 ball Kitty
Smith Smith Smith
2 Hugo 2 1 2 Hugo 2 1 John John John
mouse Kitty mouse Kitty mouse Kitty
3 Sam 2 2 3 Sam 2 2 Smith Smith Smith
4 Misty NULL 1 4 Misty NULL 1 Danielle Danielle Danielle
ball Sam ball Sam ball Sam
Davis Davis Davis
Each occurrence of the table must be given a different alias. Each column reference must be preceded with an mouse Misty NULL mouse Misty NULL
appropriate table alias. spring NULL NULL

SELECT
child.cat_name AS child_name, child_name mom_name
mom.cat_name AS mom_name Hugo Kitty
FROM cat AS child Sam Hugo JOIN WITH MULTIPLE CONDITIONS
JOIN cat AS mom Misty Kitty
You can use multiple JOIN conditions using the ON keyword once and the AND keywords as many times as you
ON child.mom_id = mom.cat_id; need.

NON-EQUI SELF JOIN CAT AS c OWNER AS o


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
TOY AS a TOY AS b 2 Hugo 1 2 10 2 Danielle Davis 10
toy_id toy_name cat_id cat_id toy_id toy_name 3 Sam 2 2 5
3 mouse 1 1 3 mouse 4 Misty 1 NULL 11
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;

LearnSQL.com is owned by Vertabelo SA


Try out the interactive SQL JOINs course at LearnSQL.com, and check out our other SQL courses. vertabelo.com | CC BY-NC-ND Vertabelo SA
SQL Window Functions Cheat Sheet
WINDOW FUNCTIONS AGGREGATE FUNCTIONS VS. WINDOW FUNCTIONS PARTITION BY ORDER BY
compute their result based on a sliding unlike aggregate functions, window functions do not collapse rows. divides rows into multiple groups, called partitions, to specifies the order of rows in each partition to which the
window frame, a set of rows that are which the window function is applied. window function is applied.
somehow related to the current row. PARTITION BY city PARTITION BY city ORDER BY month
Aggregate Functions Window Functions month city sold month city sold sum sold city month sold city month
∑ 1 Rome 200 1 Paris 300 800 200 Rome 1 300 Paris 1
2 Paris 500 2 Paris 500 800 500 Paris 2 500 Paris 2
∑ 1 London 100 1 Rome 200 900 100 London 1 200 Rome 1
current
row 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

Default Partition: with no PARTITION BY clause, the Default ORDER BY: with no ORDER BY clause, the order of
SYNTAX entire result set is the partition. rows within each partition is arbitrary.

SELECT city, month, SELECT <column_1>, <column_2>, WINDOW FRAME


sum(sold) OVER ( <window_function>() OVER ( is a set of rows that are somehow related to the current row. The window frame is evaluated separately within each partition.
PARTITION BY city PARTITION BY <...>
ORDER BY month ORDER BY <...> ROWS | RANGE | GROUPS BETWEEN lower_bound AND upper_bound
RANGE UNBOUNDED PRECEDING) total <window_frame>) <window_column_alias>
PARTITION UNBOUNDED
FROM sales; FROM <table_name>; PRECEDING The bounds can be any of the five options:
N PRECEDING
∙ 
UNBOUNDED PRECEDING
N ROWS
Named Window Definition ∙ 
n PRECEDING
CURRENT
ROW ∙ 
CURRENT ROW
M ROWS ∙ 
n FOLLOWING
SELECT country, city, SELECT <column_1>, <column_2>, M FOLLOWING ∙ 
UNBOUNDED FOLLOWING
rank() OVER country_sold_avg <window_function>() OVER <window_name> UNBOUNDED
FROM sales FROM <table_name> FOLLOWING The lower_bound must be BEFORE the upper_bound
WHERE month BETWEEN 1 AND 6 WHERE <...>
GROUP BY country, city GROUP BY <...>
ROWS BETWEEN 1 PRECEDING RANGE BETWEEN 1 PRECEDING GROUPS BETWEEN 1 PRECEDING
HAVING sum(sold) > 10000 HAVING <...> AND 1 FOLLOWING AND 1 FOLLOWING AND 1 FOLLOWING
WINDOW country_sold_avg AS ( WINDOW <window_name> AS ( city sold month city sold month city sold month
PARTITION BY country PARTITION BY <...> Paris 300 1 Paris 300 1 Paris 300 1
ORDER BY avg(sold) DESC) ORDER BY <...> Rome 200 1 Rome 200 1 Rome 200 1
ORDER BY country, city; <window_frame>) Paris 500 2 Paris 500 2 Paris 500 2
ORDER BY <...>; current
Rome 100 4
current
Rome 100 4
current
Rome 100 4
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
PARTITION BY, ORDER BY, and window frame definition are all optional. London 200 5 London 200 5 London 200 5
London 100 6 London 100 6 London 100 6
Rome 300 6 Rome 300 6 Rome 300 6
LOGICAL ORDER OF OPERATIONS IN SQL 1 row before the current row and values in the range between 3 and 5 1 group before the current row and 1 group
1 row after the current row ORDER BY must contain a single expression after the current row regardless of the value

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
n PRECEDING BETWEEN n PRECEDING AND CURRENT ROW AND CURRENT ROW.
You can use window functions in SELECT and ORDER BY. However, you can’t put window functions CURRENT ROW BETWEEN CURRENT ROW AND CURRENT ROW Without ORDER BY, the frame specification
anywhere in the FROM, WHERE, GROUP BY, or HAVING clauses. n FOLLOWING BETWEEN AND CURRENT ROW AND n FOLLOWING
is ROWS BETWEEN UNBOUNDED PRECEDING
UNBOUNDED FOLLOWING BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING AND UNBOUNDED FOLLOWING.

LearnSQL.com is owned by Vertabelo SA


Try out the interactive Window Functions course at LearnSQL.com, and check out our other SQL courses. vertabelo.com | CC BY-NC-ND Vertabelo SA
SQL Window Functions Cheat Sheet
LIST OF WINDOW RANKING FUNCTIONS DISTRIBUTION FUNCTIONS
FUNCTIONS ∙ row_number() − unique number for each row within partition, with different ∙ 
percent_rank() − the percentile ranking number of a row—a value in [0, 1] interval:
numbers for tied values (rank - 1) / (total number of rows - 1)
Aggregate Functions
∙ rank() − ranking within partition, with 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
∙ avg() rows with values less than or equal to the current row’s value divided by the total number of rows;
∙ dense_rank() − ranking within partition, with no gaps and same ranking for tied values
∙ count() a value in (0, 1] interval
∙ max() row_number rank dense_rank
city price percent_rank() OVER(ORDER BY sold) cume_dist() OVER(ORDER BY sold)
∙ 
min() over(order by price)
Paris 7 1 1 1 city sold percent_rank city sold cume_dist
∙ sum() 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 Berlin 8.5 4 3 2 Rome 200 0.5 Rome 200 0.8
∙ row_number() Moscow 9 5 5 3 Moscow 200 0.5 without this row 50% of Moscow 200 0.8 80% of values are
Madrid 10 6 6 4 values are less than this less than or equal
∙ rank() London 300 1
row’s value
London 300 1
to this one
Oslo 10 7 6 4
∙ dense_rank()
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
Distribution Functions row_number() does not require ORDER BY. Ranking functions do not accept window definition (ROWS, RANGE, GROUPS).
∙ percent_rank() frame definition (ROWS, RANGE, GROUPS).
∙ cume_dist()
Analytic Functions ANALYTIC FUNCTIONS ∙ 
first_value(expr) − the value for the first row within the window frame
∙ lead() ∙ 
lead(expr, offset, default) − the value for the row offset rows after the current; offset ∙ 
last_value(expr) − the value for the last row within the window frame
∙ lag() and 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 first_value(sold) OVER last_value(sold) OVER
and default are optional; default values: offset = 1, default = NULL (PARTITION BY city ORDER BY month) (PARTITION BY city ORDER BY month
∙ first_value() RANGE BETWEEN UNBOUNDED PRECEDING
∙ last_value() lead(sold) OVER(ORDER BY month) lag(sold) OVER(ORDER BY month) city month sold first_value AND UNBOUNDED FOLLOWING)
Paris 1 500 500
∙ nth_value() 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
3 400 100 3 400 300 Paris 3 400 400
AGGREGATE FUNCTIONS 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
2 300 100 2 300 0
FOLLOWING with last_value(). With the default
frame 3 400 500 3 400 500 window frame for ORDER BY, RANGE UNBOUNDED
offset=2

4 100 0 4 100 300 PRECEDING, last_value() returns the value for


∙ 
max(expr) − maximum value
 5 500 0 5 500 400 the current row.
within the window frame
∙ 
ntile(n) − divide rows within a partition as equally as possible into n groups, and assign ∙ 
nth_value(expr, n) − the value for the n-th row within the window frame; n must be an integer
∙ 
min(expr) − minimum value
each 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 city sold city month sold nth_value
within the window frame Rome 100 1 Paris 1 500 300
Paris 100 11 1 Paris 2 300 300
London 200 1 Paris 3 400 300
ORDER BY and Window Frame: Moscow 200 2 Rome 2 200 300
Berlin 200 22 2
ORDER BY and Window Frame: ntile(), ORDER BY and Window Frame: first_value(),
Aggregate functions do not Rome 3 300 300
Madrid 300 2 lead(), and lag() require an ORDER BY. last_value(), and nth_value() do not
require an ORDER BY. They accept Rome 4 500 300
Oslo 300 3 They do not accept window frame definition Rome 5 300 300 require an ORDER BY. They accept window frame
window frame definition (ROWS, 33
Dublin 300 3 (ROWS, RANGE, GROUPS). London 1 100 NULL definition (ROWS, RANGE, GROUPS).
RANGE, GROUPS).

LearnSQL.com is owned by Vertabelo SA


Try out the interactive Window Functions course at LearnSQL.com, and check out our other SQL courses. vertabelo.com | CC BY-NC-ND Vertabelo SA

You might also like