- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL FULL OUTER JOIN
The FULL OUTER JOIN (often referred to simply as FULL JOIN) is the most inclusive join in SQL. It acts as a combination of both a LEFT JOIN and a RIGHT JOIN. While an INNER JOIN only shows you the "overlap" where data exists in both tables, a FULL OUTER JOIN ensures that no data is left behind from either side.
When you run this join, SQL returns every record from both tables. If a row in Table A matches a row in Table B, they are joined together. If there is no match, the result set still includes that row, but fills the columns from the "missing" side with NULL values.
Syntax of FULL OUTER JOIN
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.column = table2.column;
table1: The "Left" table in your query.table2: The "Right" table you are joining.column: The common key (usually a Primary Key and Foreign Key) used to link the two datasets.
FULL OUTER JOIN syntax directly. For example, MySQL does not support it. In MySQL, you typically have to achieve this by using a LEFT JOIN and a RIGHT JOIN combined with a UNION.
Example
To understand how this works in a real-world scenario, let's look at a typical e-commerce database. We have a list of registered Customers and a list of Orders.
Customers Table
Orders Table
FULL OUTER JOIN Query
Imagine we want a report of all activity. We want to see every customer (even those who haven't ordered yet) and every order (even those placed by guests who aren't in our customers table). We use the following query:
SELECT customers.name, orders.order_id, orders.order_date
FROM customers
FULL OUTER JOIN orders
ON customers.customer_id = orders.customer_id;
Output:
Explanation of the Results:
- Alice & Bob: These customers have IDs that exist in both tables. Their data is merged perfectly.
- Charlie: Charlie is in our
Customerstable but hasn't placed an order. Because it's aFULL OUTER JOIN, he is included, but the order columns areNULL. - Order 103: This order was placed by
customer_id = 4. However, there is no customer with ID 4 in ourCustomerstable. This is an "orphaned" row, likely from a guest checkout or a data error. It is included, but thenamecolumn isNULL.
FROM customers AS c) to keep your query readable, especially when selecting columns with identical names from both tables.
FULL OUTER JOIN with WHERE Clause
One of the most powerful uses of a FULL OUTER JOIN is to identify records that do not have a match in the other table. This is effectively the opposite of an Inner Join.
SELECT customers.name, orders.order_id, orders.order_date
FROM customers
FULL OUTER JOIN orders
ON customers.customer_id = orders.customer_id
WHERE customers.customer_id IS NULL OR orders.customer_id IS NULL;
Output:
This result is incredibly helpful for database cleanup. It shows you exactly which customers have never ordered and which orders are missing customer profiles.
WHERE table.column = NULL. In SQL, you must use IS NULL to check for null values, as NULL is not equal to anything, not even itself.
FULL OUTER JOIN with Multiple Tables
You can expand this logic to three or more tables. For example, if you want to see the entire relationship chain between Customers, Orders, and Payments:
SELECT
c.name,
o.order_id,
p.payment_date
FROM customers c
FULL OUTER JOIN orders o ON c.customer_id = o.customer_id
FULL OUTER JOIN payments p ON o.order_id = p.order_id;
In this scenario, SQL will first perform a full join between customers and orders, and then perform another full join between that result and the payments table. This helps you find customers who haven't ordered, orders that haven't been paid, or even payments that aren't linked to a valid order.
Key Points
- FULL OUTER JOIN captures 100% of the data from both tables involved in the join.
- It is essentially a UNION of a Left Join and a Right Join.
- NULL values are the engine's way of saying "I found a row here, but there was no corresponding data in the other table."
- It is most frequently used in Reporting, Data Warehousing, and Data Validation tasks.
Summary
The FULL OUTER JOIN is your "safety net" join. It ensures that you don't lose records just because they don't have a match. Whether you are reconciling financial accounts or checking for data integrity between two systems, the Full Outer Join provides the visibility you need to see the "big picture" of your data.