SQL Joins
SQL Joins
coding_knowledge
SQL JOINS
01
JOINING TABLES
JOIN combines data from two tables.
TOY CAT
t o y_id toy_name cat_id cat_id cat_name
1 ball 3 1 Kitty
2 spring NULL 2 Hugo
3 m o use 1 3 Sam
4 m o use 4 4 Misty
5 ball 1
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 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.
JOIN
JOIN returns all rows that match the ON
condition. JOIN is also calledINNER JOIN.
SELECT *
FROM toy toy_id toy_name cat_id cat_id cat_name
JOIN cat 5 ball 1 1 Kitt y
ON toy.cat_id = cat.cat_id; 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 toy, cat
WHERE toy.cat_id = cat.cat_id;
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: 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
joined tables.
NATURAL JOIN
If the tables have columns with the same name, you can use
NATURAL in JOIN stead of JOIN. cat_id t o y_id toy_name cat_name
1 5 ball Kitty
SELECT * 1 3 mouse Kitty
FROM toy 3 1 ball Sam
NATURAL JOIN cat; 4 4 mouse Misty
The common column appears only once in the result table.
Note: NATURAL JOIN is rarely used in real life.
LEFT JOIN 02
LEFT JOIN returns all rows from the left table with matching rows from the right table. Rows without a match are filled
with NULL s. LEFT JOIN is also called LEFT OUTER JOIN.
SELECT * toy_id toy_name cat_id cat_id cat_name
FROM toy 5 ball 1 1 Kitt y
m o use 1 1 Kitty
LEFT JOIN cat 3 ball 3 3 Sam
ON toy.cat_id = cat.cat_id; 1 m o use 4 4 Misty
4 spring NULL NULL NULL
2 whole left table
RIGHT JOIN
RIGHT JOIN returns all rows from the right table with matching rows from the left table. Rows without a match are
filled with NULL s. RIGHT JOIN is also called RIGHT OUTER JOIN.
SELECT * t o y_id toy_name cat_id cat_id cat_name
FROM toy 5 ball 1 1 Kitt y
RIGHT JOIN cat 3 m o use 1 1 Kitty
NULL NULL NULL 2 Hugo
ON toy.cat_id = cat.cat_id; ball 3 3 Sam
1
4 m o use 4 4 Misty
whole right table
FULL JOIN
FULL JOIN returns all rows from the left table and all rows from the right table. It fills the non-matching rows with
NULLs. FULL JOINis also called FULL OUTER JOIN.
SELECT * t o y_id toy_name cat_id cat_id cat_name
FROM toy 5 ball 1 1 Kitt y
FULL JOIN cat 3 m o use 1 1 Kitty
NULL NULL NULL 2 Hugo
ON toy.cat_id = cat.cat_id; 1 ball 3 3 Sam
4 m o use 4 4 Misty
2 spring NULL NULL NULL
whole left table whole right table
CROSS JOIN
CROSS JOINreturns all possible combinations of rows from the left and right tables.
SELECT * t o y_id toy_name cat_id cat_id cat_name
FROM toy 1 ball 3 1 Kitty
CROSS JOIN cat; 2 spring NULL 1 Kitty
3 m o use 1 1 Kitty
Other syntax: 4 mouse 4 1 Kitty
SELECT * 5 ball 1 1 Kitty
FROM toy, cat; 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
··· ··· ··· ··· ···
COLUMN AND TABLE ALIASES 03
Aliases give a temporary name to a table or a column in a table.
CAT AS c OWNER AS o
cat_id cat_name mom_id owner_id id name
1 Kitty 5 1 1 John Smith
2 Hugo 1 2 2 Danielle Davis
3 Sam 2 2
4 Misty 1 NULL
A column alias renames a column in the result. A table alias renames a table within the query. If you define a table alias,
you must use it instead of the table name everywhere in the query. The AS keyword is optional in defining aliases.
SELECT
o.name AS owner_name, cat_name owner_name
c.cat_name Kitty John Smith
FROM cat AS c Sam Danielle Davis
JOIN owner AS o Hugo Danielle Davis
ON c.owner_id = o.id;
SELF JOIN
You can join a table to itself, for example, to show a parent-child relationship.
CAT AS child CAT AS mom
cat_id cat_nameowner_id mom_id cat_id cat_nameowner_id mom_id
1 Kitty 1 5 1 Kitty 1 5
2 Hugo 2 1 2 Hugo 2 1
3 Sam 2 2 3 Sam 2 2
4 Misty NULL 1 4 Misty NULL 1
Each occurrence of the table must be given a different alias. Each column reference must be preceded with an
appropriate table alias.
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 cat AS mom Misty Kitty
ON child.mom_id = mom.cat_id;
TOY AS a TOY AS b
toy_id toy_name cat_id cat_id toy_id toy_name
3 mouse 1 1 3 mouse
5 ball 1 1 5 ball
1 ball 3 3 1 ball
4 mouse 4 4 4 mouse
2 spring NULL NULL 2 spring
SELECT cat_a_id toy_a cat_b_id toy_b
a.toy_name AS toy_a, 1
1 mouse 3 ball
b.toy_name AS toy_b ball 3 ball
FROM toy a 1
1 mouse 4 mouse
JOIN toy b 3 ball 4 mouse
ON a.cat_id < b.cat_id; ball 4 mouse
04
MULTIPLE JOINS
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.
TOY AS t CAT AS c
toy_id toy_name cat_id OWNER AS o
3 cat_id cat_name mom_id owner_id
1 ball 1 Kitty 5 1 id name
2 spring NULL 1 JohnSmith
1 2 Hugo 1 2
3 mouse 3 Sam 2 2 DanielleDavi
4 mouse 4 2 s
1 4 Misty 1 NULL
5 ball
JOIN & JOIN JOIN & LEFT JOIN LEFT JOIN & LEFT JOIN
SELECT t.toy_name, SELECT t.toy_name, SELECT
c.cat_name, o.name AS c.cat_name, o.name AS t.toy_name,
owner_name FROM toy t owner_name FROM toy t c.cat_name,
JOIN cat c ON t.cat_id = JOIN cat c ON t.cat_id = o.name AS owner_name
c.cat_id JOIN owner o ON c.cat_id LEFT JOIN owner o FROM toy t
c.owner_id = o.id; ON c.owner_id = o.id; LEFT JOIN cat c
ON t.cat_id = c.cat_id
LEFT JOIN owner o
ON c.owner_id = o.id;
toy_name cat_name owner_name toy_name cat_name owner_name toy_name cat_name owner_name
ball Kitty John Smith ball Kitty John Smith ball Kitty John Smith
mouse Kitty John Smith mouse Kitty John Smith mouse Kitty John Smith
ball Sam Danielle Davis ball Sam Danielle Davis ball Sam Danielle Davis
mouse Misty NULL mouse Misty NULL
spring NULL NULL
CAT AS c OWNER AS o
cat_id cat_name mom_id owner_id age id name age
1 Kitty 5 1 17 1 John Smith 18
2 Hugo 1 2 10 2 Danielle Davis 10
3 Sam 2 2 5
4 Misty 1 NULL 11
SELECT
cat_name,
o.name AS owner_name,
c.age AS cat_age, cat_name owner_name age age
o.age AS owner_age Kitty John Smith 17 18
FROM cat c Sam Danielle Davis 5 10
JOIN owner o
ON c.owner_id = o.id
AND c.age < o.age;