In Lesson 5, we learned how to use INNER JOINs
to combine data from multiple tables. INNER JOIN
only returns records when there is a match in both tables. But what if you want to see ALL records from one table, even when there's no match in the other table?
This is where OUTER JOINs
become essential. Think of them as more inclusive joins that don't leave any records behind.
OUTER JOINs
return all records from one table (or both tables), and the matching records from the other table. When there is no match, the result will show NULL
values for the missing data.
The three types of OUTER JOINs:
Basic OUTER JOIN Syntax:
SELECT column_name(s)
FROM table1
LEFT/RIGHT/FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
The LEFT JOIN
keyword returns all records from the left table, and the matching records from the right table. The result is NULL
from the right side when there is no match.
LEFT JOIN Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Example with sample data:
Companies Table (Left):
company_id | company_name | city
-----------|-----------------|----------
1 | Tech Solutions | New York
2 | Global Corp | London
3 | StartUp Inc | Austin
Orders Table (Right):
order_id | company_id | order_amount | order_date
---------|------------|--------------|------------
101 | 1 | 5000 | 2024-01-15
102 | 1 | 3000 | 2024-02-20
103 | 2 | 7500 | 2024-03-10
LEFT JOIN Result:
SELECT companies.company_name, orders.order_amount
FROM companies
LEFT JOIN orders ON companies.company_id = orders.company_id;
company_name | order_amount
----------------|-------------
Tech Solutions | 5000
Tech Solutions | 3000
Global Corp | 7500
StartUp Inc | NULL
💡 Key Point: Notice how "StartUp Inc" appears in the result even though it has no orders. This is the power of LEFT JOIN - it keeps all records from the left table.
The RIGHT JOIN
keyword returns all records from the right table, and the matching records from the left table. The result is NULL
from the left side when there is no match.
RIGHT JOIN Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Example:
SELECT companies.company_name, orders.order_amount
FROM companies
RIGHT JOIN orders ON companies.company_id = orders.company_id;
This ensures all orders are included, even if company information is missing.
The FULL OUTER JOIN
keyword returns all records when there is a match in either the left or right table. It combines the results of both LEFT
and RIGHT JOINs
.
FULL OUTER JOIN Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
Example:
SELECT companies.company_name, orders.order_amount
FROM companies
FULL OUTER JOIN orders ON companies.company_id = orders.company_id;
This returns all companies (even without orders) and all orders (even without company info).
OUTER JOINs
often produce NULL
values in the results. You can use these techniques to work with NULLs
:
Finding records with missing data:
SELECT companies.company_name, orders.order_amount
FROM companies
LEFT JOIN orders ON companies.company_id = orders.company_id
WHERE orders.order_amount IS NULL;
Finding records with existing data:
SELECT companies.company_name, orders.order_amount
FROM companies
LEFT JOIN orders ON companies.company_id = orders.company_id
WHERE orders.order_amount IS NOT NULL;
Filtering based on NULL values:
-- Companies that have never placed an order
SELECT company_name
FROM companies
LEFT JOIN orders ON companies.company_id = orders.company_id
WHERE orders.company_id IS NULL;
OUTER JOINs
are useful when you need to:
Common business scenarios:
-- Find all customers, including those who haven't made purchases
SELECT customers.name, orders.total_amount
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;
-- Find all products, including those that haven't been sold
SELECT products.product_name, sales.quantity_sold
FROM products
LEFT JOIN sales ON products.product_id = sales.product_id;
💡 Did You Know? You might see OUTER JOINs
written as LEFT OUTER JOIN
, RIGHT OUTER JOIN
, or FULL OUTER JOIN
. The OUTER keyword is optional - LEFT JOIN
, RIGHT JOIN
, and FULL JOIN
mean exactly the same thing. Most databases support LEFT JOIN
and RIGHT JOIN
, but FULL OUTER JOIN
support varies between database systems.
In this lesson's exercises, you'll work with a Companies and Orders dataset. Some companies in our business directory haven't placed any orders yet, and some orders might have incomplete company information.
This real-world scenario will help you understand when OUTER JOINs
are more useful than INNER JOINs
for complete data analysis. You'll practice:
Take your time with these exercises - understanding OUTER JOINs is crucial for real-world data analysis where incomplete data is common!
Ready to practice SQL? Try our coding challenges →