Try out the interactive SQL JOINs course at LearnSQL.com, and check out our other SQL courses.
LearnSQL.com is owned by Vertabelo SA
vertabelo.com | CC BY-NC-ND Vertabelo SA
SQL JOINs Cheat Sheet
NATURAL JOIN
If the tables have columns with the same name, you can use
NATURAL JOIN instead of JOIN.
The common column appears only once in the result table.
Note: NATURAL JOIN is rarely used in real life.
SELECT *
FROM toy
NATURAL JOIN cat;
cat_id toy_id toy_name cat_name
1 5 ball Kitty
1 3 mouse Kitty
3 1 ball Sam
4 4 mouse Misty
LEFT JOIN
LEFT JOIN returns all rows from the le table with matching rows from the right table. Rows without a match are lled
with NULLs. LEFT JOIN is also called LEFT OUTER JOIN.
SELECT *
FROM toy
LEFT JOIN cat
ON toy.cat_id = cat.cat_id;
toy_id toy_nam e cat_id cat_id cat_name
5 ball 1 1 Kitty
3 mouse 1 1 Kitty
1 ball 3 3 Sam
4 mouse 4 4 Misty
2 spring
NULL NULL NULL
whole le table
RIGHT JOIN
RIGHT JOIN returns all rows from the right table with matching rows from the le table. Rows without a match are
lled with NULLs. RIGHT JOIN is also called RIGHT OUTER JOIN.
SELECT *
FROM toy
RIGHT JOIN cat
ON toy.cat_id = cat.cat_id;
toy_id toy_nam e cat_id cat_id cat_name
5 ball 1 1 Kitty
3 mouse 1 1 Kitty
NULL NULL NULL 2 Hugo
1 ball 3 3 Sam
4 mouse 4
4 Misty
whole right table
FULL JOIN
FULL JOIN returns all rows from the le table and all rows from the right table. It lls the non-matching rows with
NULLs. FULL JOIN is also called FULL OUTER JOIN.
SELECT *
FROM toy
FULL JOIN cat
ON toy.cat_id = cat.cat_id;
toy_id toy_nam e cat_id cat_id cat_name
5 ball 1 1 Kitty
3 mouse 1 1 Kitty
NULL NULL NULL 2 Hugo
1 ball 3 3 Sam
4 mouse 4 4 Misty
2 spring
NULL NULL NULL
whole le table whole right table
CROSS JOIN
CROSS JOIN returns all possible combinations of rows from the le and right tables.
toy_id toy_nam e cat_id cat_id cat_name
1 ball 3 1 Kitty
2 spring NULL 1 Kitty
3 mouse 1 1 Kitty
4 mouse 4 1 Kitty
5 ball 1 1 Kitty
1 ball 3 2 Hugo
2 spring NULL 2 Hugo
3 mouse 1 2 Hugo
4 mouse 4 2 Hugo
5 ball 1 2 Hugo
1 ball 3 3 Sam
··· ··· ··· ··· ···
SELECT *
FROM toy
CROSS JOIN cat;
SELECT *
FROM t o y, c a t;
Other syntax:
JOIN
JOIN returns all rows that match the ON condition. JOIN is also called INNER JOIN.
SELECT *
FROM toy
JOIN cat
ON toy.cat_id = cat.cat_id;
toy_id toy_nam e cat_id cat_id cat_name
5 ball 1 1 Kitty
3 mouse 1 1 Kitty
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.
SELECT *
FROM t o y, c a t
WHERE toy.cat_id = cat.cat_id;
JOIN combines data from two tables.
JOINING TABLES
JOIN typically combines rows with equal values for the specied columns. Usually, one table contains a primary key,
which is a column or columns that uniquely identify rows in the table (the c at _id column in the cat table).
The other table has a column or columns that refer to the primary key columns in the rst table (the cat_id column in
the toy table). Such columns are foreign keys. The JOIN condition is the equality between the primary key columns in
one table and columns referring to them in the other table.
CAT
cat_id cat_name
1 Kitty
2 Hugo
3 Sam
4 Misty
TOY
toy_id toy_nam e cat _id
1 ball 3
2 spring NULL
3 mouse 1
4 mouse 4
5 ball 1
JOIN CONDITIONS
The JOIN condition doesn't have to be an equality – it can be any condition you want. JOIN doesn't interpret the JOIN
condition, it only checks if the rows satisfy the given condition.
To refer to a column in the JOIN query, you have to use the full column name: rst 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
joined tables.