SQL Joins Cheat Sheet
SQL Joins Cheat Sheet
With this SQL Joins cheat sheet, you'll have a handy reference guide to joining data in SQL.
Jul 28, 2022 · 6 min read
Share
SQL is a powerful tool for searching through large amounts of data and returning specific
information for analysis. Learning SQL is important for anyone aspiring to be a data analyst,
data engineer, or data scientist, and it's helpful in many other fields, such as web development or
marketing.
In this cheat sheet, you'll find a handy list of functions covering SQL Joins —all collected from
our SQL for Joining Data Course.
Have this cheat sheet at your fingertips
Album table
album_id title artist_id
1 For those who rock 1
2 Dream on 2
3 Restless and wild 2
4 Let there be rock 1
5 Rumours 6
SQL Joins Cheat Sheet
INNER JOINS
An inner join between two tables will return only records where a joining field, such as a key,
finds a match in both tables.
SELECT *
FROM ARTIST AS ART
INNER JOIN ALBUM AS ALB
ON ART.ARTIST_ID = ALB.ARTIST_ID;
Powered By
SELECT *
FROM ARTIST AS ART
INNER JOIN ALBUM AS ALB
USING (ARTIST_ID);
Powered By
SELF JOIN
Self-joins are used to compare values in a table to other values of the same table by joining
different parts of a table together.
SELECT
alb1.artist_id,
alb1.title AS alb1_title,
alb2.title AS alb2_title
FROM album as alb1
INNER JOIN album as alb2
ON art1.artist_id = art2.artist_id;
Powered By
LEFT JOIN
A left join keeps all of the original records in the left table and returns missing values for any
columns from the right table where the joining field did not find a match.
SELECT *
FROM ARTIST AS ART
LEFT JOIN ALBUM AS ALB
ON ART.ARTIST_ID = ALB.ARTIST_ID;
Powered By
RIGHT JOIN
A right join keeps all of the original records in the right table and returns missing values for any
columns from the left table where the joining field did not find a match. Right joins are far less
common than left joins, because right joins can always be re-written as left joins.
SELECT *
FROM ARTIST AS ART
RIGHT JOIN ALBUM AS ALB
ON ART.ARTIST_ID = ALB.ARTIST_ID;
Powered By
FULL JOIN
A full join combines a left join and right join. A full join will return all records from a table,
irrespective of whether there is a match on the joining field in the other table, returning null
values accordingly.
SELECT *
FROM ARTIST AS ART
FULL OUTER JOIN ALBUM AS ALB
ON ART.ARTIST_ID = ALB.ARTIST_ID;
Powered By
CROSS JOIN
CROSS JOIN creates all possible combinations of two tables. CROSS JOIN does not require a field
to join ON.
UNION
The UNION operator is used to vertically combine the results of two SELECT statements. For
UNION to work without errors, all SELECT statements must have the same number of columns and
corresponding columns must have the same data type. UNION does not return duplicates.
SELECT ARTIST_ID
FROM ARTIST
UNION
SELECT ARTIST_ID
FROM ALBUM;
Powered By
UNION ALL
The UNION ALL operator works just like UNION, but it returns duplicate values. The same
restrictions of UNION hold true for UNION ALL
SELECT ARTIST_ID
FROM ARTIST
UNION ALL
SELECT ARTIST_ID
FROM ALBUM;
Powered By
INTERSECT
The INTERSECT operator returns only identical rows from two tables.
SELECT ARTIST_ID
FROM ARTIST
INTERSECT
SELECT ARTIST_ID
FROM ALBUM;
Powered By
EXCEPT
The EXCEPT operator returns only those rows from the left table that are not present in the right
table.
SELECT ARTIST_ID
FROM ARTIST
EXCEPT
SELECT ARTIST_ID
FROM ALBUM;
Powered By
SEMI JOIN
A semi join chooses records in the first table where a condition is met in the second table. A semi
join makes use of a WHERE clause to use the second table as a filter for the first.
SELECT *
FROM ALBUM
WHERE ARTIST_ID IN
(SELECT ARTIST_ID
FROM ARTIST);
Powered By
ANTI JOIN
The anti join chooses records in the first table where a condition is NOT met in the second table. It
makes use of a WHERE clause to use exclude values from the second table.
SELECT *
FROM ALBUM
WHERE ARTIST_ID NOT IN
(SELECT ARTIST_ID
FROM ARTIST);