SQL Joins
SQL Joins
This SQL tutorial explains how to use SQL joins with syntax, visual illustrations, and examples.
DESCRIPTION
SQL JOINS are used to retrieve data from multiple tables. A SQL JOIN is performed whenever
two or more tables are joined in a SQL statement.
There are 4 different types of SQL joins:
So let's discuss SQL JOIN syntax, look at visual illustrations of SQL JOINS, and explore SQL
JOIN examples.
Chances are, you've already written a SQL statement that uses an SQL INNER JOIN. It is the
most common type of SQL join. SQL INNER JOINS return all rows from multiple tables where
the join condition is met.
Syntax
Visual Illustration
In this visual diagram, the SQL INNER JOIN returns the shaded area:
The SQL INNER JOIN would return the records where table1 and table2 intersect.
Example
This SQL INNER JOIN example would return all rows from the suppliers and orders tables
where there is a matching supplier_id value in both the suppliers and orders tables.
Let's look at some data to explain how the INNER JOINS work:
We have a table called suppliers with two fields (supplier_id and supplier_name). It contains the
following data:
supplier_id supplier_name
10000 IBM
10002 Microsoft
10003 NVIDIA
We have another table called orders with three fields (order_id, supplier_id, and order_date). It
contains the following data:
order_id supplier_id order_date
Old Syntax
As a final note, it is worth mentioning that the SQL INNER JOIN example above could be
rewritten using the older implicit syntax as follows (but we still recommend using the INNER
JOIN keyword syntax):
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
Another type of join is called a LEFT OUTER JOIN. This type of join returns all rows from the
LEFT-hand table specified in the ON condition and only those rows from the other table where
the joined fields are equal (join condition is met).
Syntax
In some databases, the LEFT OUTER JOIN keywords are replaced with LEFT JOIN.
Visual Illustration
In this visual diagram, the SQL LEFT OUTER JOIN returns the shaded area:
The SQL LEFT OUTER JOIN would return the all records from table1 and only those records
from table2 that intersect with table1.
Example
This LEFT OUTER JOIN example would return all rows from the suppliers table and only those
rows from the orders table where the joined fields are equal.
If a supplier_id value in the suppliers table does not exist in the orders table, all fields in the
orders table will display as <null> in the result set.
Let's look at some data to explain how LEFT OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and supplier_name). It contains the
following data:
supplier_id supplier_name
10000 IBM
10002 Microsoft
10003 NVIDIA
We have a second table called orders with three fields (order_id, supplier_id, and order_date). It
contains the following data:
order_id supplier_id order_date
If we run the SQL statement (that contains a LEFT OUTER JOIN) below:
SELECT SUPPLIERS .supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
LEFT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
The rows for Microsoft and NVIDIA would be included because a LEFT OUTER JOIN was used.
However, you will notice that the order_date field for those records contains a <null> value.
Old Syntax
As a final note, it is worth mentioning that the LEFT OUTER JOIN example above could be
rewritten using the older implicit syntax that utilizes the outer join operator (+) as follows (but
we still recommend using the LEFT OUTER JOIN keyword syntax):
SELECT SUPPLIERS .supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id(+);
Another type of join is called a SQL RIGHT OUTER JOIN. This type of join returns all rows
from the RIGHT-hand table specified in the ON condition and only those rows from the other
table where the joined fields are equal (join condition is met).
Syntax
In some databases, the RIGHT OUTER JOIN keywords are replaced with RIGHT JOIN.
Visual Illustration
In this visual diagram, the SQL RIGHT OUTER JOIN returns the shaded area:
The SQL RIGHT OUTER JOIN would return the all records from table2 and only those records
from table1 that intersect withtable2.
Example
This RIGHT OUTER JOIN example would return all rows from the orders table and only those
rows from the suppliers table where the joined fields are equal.
If a supplier_id value in the orders table does not exist in the suppliers table, all fields in the
suppliers table will display as <null> in the result set.
Let's look at some data to explain how RIGHT OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and supplier_name). It contains the
following data:
supplier_id supplier_name
10000 Apple
10001 Google
We have a second table called orders with three fields (order_id, supplier_id, and order_date). It
contains the following data:
order_id supplier_id order_date
If we run the SQL statement (that contains a RIGHT OUTER JOIN) below:
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers
RIGHT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
The row for 500127 (order_id) would be included because a RIGHT OUTER JOIN was used.
However, you will notice that the supplier_name field for that record contains a <null> value.
Old Syntax
As a final note, it is worth mentioning that the RIGHT OUTER JOIN example above could be
rewritten using the older implicit syntax that utilizes the outer join operator (+) as follows (but
we still recommend using the RIGHT OUTER JOIN keyword syntax):
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers, orders
WHERE suppliers.supplier_id(+) = orders.supplier_id;
Another type of join is called a SQL FULL OUTER JOIN. This type of join returns all rows from
the LEFT-hand table and RIGHT-hand table with nulls in place where the join condition is not
met.
Syntax
In some databases, the FULL OUTER JOIN keywords are replaced with FULL JOIN.
Visual Illustration
In this visual diagram, the SQL FULL OUTER JOIN returns the shaded area:
The SQL FULL OUTER JOIN would return the all records from both table1 and table2.
Example
This FULL OUTER JOIN example would return all rows from the suppliers table and all rows
from the orders table and whenever the join condition is not met, <nulls> would be extended to
those fields in the result set.
If a supplier_id value in the suppliers table does not exist in the orders table, all fields in the
orders table will display as <null> in the result set. If a supplier_id value in the orders table does
not exist in the suppliers table, all fields in the suppliers table will display as <null> in the result
set.
Let's look at some data to explain how FULL OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and supplier_name). It contains the
following data:
supplier_id supplier_name
10000 IBM
10002 Microsoft
10003 NVIDIA
We have a second table called orders with three fields (order_id, supplier_id, and order_date). It
contains the following data:
order_id supplier_id order_date
If we run the SQL statement (that contains a FULL OUTER JOIN) below:
SELECT SUPPLIERS .supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
FULL OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
The rows for Microsoft and NVIDIA would be included because a FULL OUTER JOIN was
used. However, you will notice that the order_date field for those records contains a <null>
value.
The row for supplier_id 10004 would be also included because a FULL OUTER JOIN was used.
However, you will notice that the supplier_id and supplier_name field for those records contain a
<null> value.
Old Syntax
As a final note, it is worth mentioning that the FULL OUTER JOIN example above could not
have been written in the old syntax without using a UNION query.
Tutorial Home