SQL Joins
SQL Joins
php
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 listed in a SQL statement.
So let's discuss SQL JOIN syntax, look at visual illustrations of SQL JOINS and explore some
examples.
Syntax
The syntax for the INNER JOIN in SQL is:
1 of 9 10-10-2022, 11:48
SQL: JOINS https://www.techonthenet.com/sql/joins.php
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
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
Let's look at an example of how to use the INNER JOIN in a query.
In this example, we have a table called customers with the following data:
2 of 9 10-10-2022, 11:48
SQL: JOINS https://www.techonthenet.com/sql/joins.php
There will be 4 records selected. These are the results that you should see:
This example would return all rows from the customers and orders tables where there is a
matching customer_id value in both the customers and orders tables.
The rows where customer_id is equal to 6000 and 9000 in the customers table would be omitted,
since they do not exist in both tables. The row where the order_id is 5 from the orders table would
be omitted, since the customer_id of NULL does not exist in the customers table.
Old Syntax
As a final note, it is worth mentioning that the INNER JOIN example above could be rewritten
using the older implicit syntax as follows (but we still recommend using the INNER JOIN keyword
syntax):
3 of 9 10-10-2022, 11:48
SQL: JOINS https://www.techonthenet.com/sql/joins.php
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
The syntax for the LEFT OUTER JOIN in SQL is:
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
In some databases, the OUTER keyword is omitted and written simply as 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
Now let's look at an example that shows how to use the LEFT OUTER JOIN in a SELECT
statement.
4 of 9 10-10-2022, 11:48
SQL: JOINS https://www.techonthenet.com/sql/joins.php
There will be 6 records selected. These are the results that you should see:
This LEFT OUTER JOIN example would return all rows from the customers table and only those
rows from the orders table where the joined fields are equal.
If a customer_id value in the customers table does not exist in the orders table, all fields in the
orders table will display as NULL in the result set. As you can see, the rows where customer_id is
6000 and 9000 would be included with a LEFT OUTER JOIN but the order_id and order_date
fields display NULL.
5 of 9 10-10-2022, 11:48
SQL: JOINS https://www.techonthenet.com/sql/joins.php
Syntax
The syntax for the RIGHT OUTER JOIN in SQL is:
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;
In some databases, the OUTER keyword is omitted and written simply as 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 with table2.
Example
Now let's look at an example that shows how to use the RIGHT OUTER JOIN in a SELECT
statement.
6 of 9 10-10-2022, 11:48
SQL: JOINS https://www.techonthenet.com/sql/joins.php
There will be 5 records selected. These are the results that you should see:
This RIGHT OUTER JOIN example would return all rows from the orders table and only those
rows from the customers table where the joined fields are equal.
If a customer_id value in the orders table does not exist in the customers table, all fields in the
customers table will display as NULL in the result set. As you can see, the row where order_id is 5
would be included with a RIGHT OUTER JOIN but the customer_id field displays NULL.
7 of 9 10-10-2022, 11:48
SQL: JOINS https://www.techonthenet.com/sql/joins.php
Syntax
The syntax for the SQL FULL OUTER JOIN is:
SELECT columns
FROM table1
FULL [OUTER] JOIN table2
ON table1.column = table2.column;
In some databases, the OUTER keyword is omitted and written simply as 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
Let's look at an example that shows how to use the FULL OUTER JOIN in a SELECT statement.
8 of 9 10-10-2022, 11:48
SQL: JOINS https://www.techonthenet.com/sql/joins.php
There will be 7 records selected. These are the results that you should see:
9 of 9 10-10-2022, 11:48