You are on page 1of 1

Learn SQL from Scratch

Description Diagram Syntax

An inner join matches each row of table1 table2 SELECT *


one table with rows of a second
col A col B col B col C FROM table1
table based on a common column.
The resulting table will only contain JOIN table2
a x = x d
rows where there is a match in both ON table1.colB = table2.colB;
b y = w e
the left and right tables.
c z = z f

INNER JOIN
col A col B col C
a x d
c z f

A left join matches each row of one table1 table2 SELECT *


table with rows of a second table
col A col B col B col C FROM table1
based on a common column. The
resulting table will only contain all LEFT JOIN table2
a x = x d
data from the first (left) table, and ON table1.colB = table2.colB;
b y = w e
additional data from the second
(right) table for where there was no c z = z f
matching information from the
right table.

LEFT JOIN
col A col B col C
a x d
b yz
c z f

A cross join combines all rows table1 table2 SELECT *


of one table with all rows of FROM table1
another table. col A col B col C col D
CROSS JOIN table2;
a c e h
b d f i
g j

CROSS JOIN
col A col B col C col D
a c e h
b d e h
a c f i
b d f i
a c g j
b d g j

The command union allows table1 table2 SELECT *


us to stack one dataset on top FROM table1
col A col B col C col D
of the other.
UNION
a c e h
SELECT *
b d f i
FROM table2;
g j

UNION
col A col B
a c
b d
e h
f i
g j

You might also like