SQL FULL OUTER JOIN, also known as FULL JOIN, is a type of join that combines the result of both LEFT JOIN and RIGHT JOIN, resulting in a joined table that contains all rows from both tables, regardless of whether there is a match or not. In this tutorial, we will explain how to use FULL OUTER JOIN and provide some examples.

SQL Full Outer Join
Syntax:
The syntax for using FULL OUTER JOIN in SQL is as follows:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
In the above syntax, table1
and table2
are the two tables we want to join. We use the FULL OUTER JOIN
keyword to indicate that we want a full outer join. column_name(s)
are the columns we want to select from the resulting joined table. We specify the columns we want to join on using the ON
keyword.
Example:
Let’s say we have two tables, orders
and customers
, which contain information about orders and customers, respectively. We want to create a join table that contains all orders and customers, regardless of whether a customer has made an order or not.
orders table
+------+-------+-------+
| id | item | price |
+------+-------+-------+
| 1 | Apple | 1.00 |
| 2 | Pear | 1.50 |
| 3 | Grape | 2.00 |
+------+-------+-------+
customers table
+------+--------+
| id | name |
+------+--------+
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie|
+------+--------+
To perform a full outer join on these two tables, we can use the following SQL query:
SELECT
*
FROM
orders
FULL OUTER JOIN customers ON orders.id = customers.id;
The resulting table will contain all rows from both tables, and any missing values will be filled with NULL.
+------+-------+-------+------+--------+
| id | item | price | id | name |
+------+-------+-------+------+--------+
| 1 | Apple | 1.00 | 1 | Alice |
| 2 | Pear | 1.50 | 2 | Bob |
| 3 | Grape | 2.00 | 3 | Charlie|
| NULL | NULL | NULL | 4 | David |
+------+-------+-------+------+--------+
As you can see, the resulting table contains all orders and customers, regardless of whether a customer has made an order or not. Any missing values are filled with NULL.
Advanced examples and notes on using SQL FULL OUTER JOIN
Here are some more advanced examples and notes on using SQL FULL OUTER JOIN:
Retrieving all records from three tables
Suppose we have three tables, customers
, orders
, and payments
, and we want to retrieve all records from all three tables. We can use FULL OUTER JOIN twice to achieve this:
SELECT
*
FROM
customers
FULL OUTER JOIN orders ON customers.id = orders.customer_id
FULL OUTER JOIN payments ON customers.id = payments.customer_id;
This will return a joined table that contains all records from all three tables.
Combining multiple columns from two tables
When joining two tables, we can use the SELECT statement to combine multiple columns from both tables. For example, let’s say we have two tables, employees
and departments
, and we want to create a joined table that contains the names of all employees and their department names. We can use the following query:
SELECT
employees.name,
departments.name
FROM
employees
FULL OUTER JOIN departments ON employees.department_id = departments.id;
This will return a joined table that contains the names of all employees and their department names.
Handling NULL values
When using FULL OUTER JOIN, we may encounter NULL values in the resulting table. It’s important to handle these NULL values correctly to ensure that our queries return accurate results. For example, if we want to count the number of orders placed by each customer, we can use the following query:
SELECT
customers.name,
COUNT(orders.id)
FROM
customers
FULL OUTER JOIN orders ON customers.id = orders.customer_id
GROUP BY
customers.name;
This will return a table that shows the number of orders placed by each customer. However, if a customer has never placed an order, their name will still appear in the table, but the COUNT value will be NULL. To handle this, we can use the COALESCE function to replace NULL values with a default value, such as 0:
SELECT
customers.name,
COALESCE(COUNT(orders.id),
0)
FROM
customers
FULL OUTER JOIN orders ON customers.id = orders.customer_id
GROUP BY
customers.name;
This will return a table that shows the number of orders placed by each customer, with a default value of 0 for customers who have never placed an order.
Performance considerations
FULL OUTER JOIN can be a computationally expensive operation, particularly when joining large tables. It’s important to optimize our queries to ensure that they run as efficiently as possible. Some tips for optimizing FULL OUTER JOIN queries include:
- Use indexes to speed up searches on large tables.
- Use WHERE clauses to filter the data as much as possible before joining tables.
- Use temporary tables or subqueries to break up complex queries into smaller, more manageable pieces.
Conclusion:
In conclusion, SQL FULL OUTER JOIN is a powerful tool for joining two tables and creating a new table that contains all rows from both tables. By understanding how to use FULL OUTER JOIN, you can manipulate your data to get the desired results. Use the syntax and examples in this tutorial as a reference for your own SQL queries.