FULL OUTER JOIN
Combine every row from two tables, filling gaps with NULLs where no match exists.
- Complete Coverage â preserves all rows from both the left and right tables
- Null Matching â unmatched columns appear as NULL in the result set
- Symmetric Result â equivalent to the UNION of LEFT JOIN and RIGHT JOIN Every row finds a home, matched or not.
What Is a FULL OUTER JOIN?
SELECT
c.customer_id,
c.first_name,
o.order_id,
o.total_amount
FROM customers c
FULL OUTER JOIN orders o
ON c.customer_id = o.customer_id;
Syntax
SELECT columns
FROM table_a
FULL [OUTER] JOIN table_b
ON table_a.column = table_b.column;
| Keyword | Purpose |
|---|---|
FULL | Includes all rows from both tables |
OUTER | Optional keyword â FULL JOIN is equivalent |
ON | Specifies the join condition (mandatory) |
How FULL OUTER JOIN Works
| Scenario | Left Row | Right Row | Result |
|---|---|---|---|
| Match found | Included | Included | Combined columns from both tables |
| No match on left | NULL | Included | Right columns only |
| No match on right | Included | NULL | Left columns only |
Basic Example
-- Show every customer and every order, including unmatched on both sides
SELECT
c.customer_id,
c.first_name,
o.order_id,
o.order_date,
o.total_amount
FROM customers c
FULL OUTER JOIN orders o
ON c.customer_id = o.customer_id
ORDER BY COALESCE(c.customer_id, 0), o.order_date;
Finding Unmatched Rows
-- Customers without any orders (NULL on right side)
-- and orders without a valid customer (NULL on left side)
SELECT
c.customer_id,
c.first_name,
o.order_id,
o.order_date
FROM customers c
FULL OUTER JOIN orders o
ON c.customer_id = o.customer_id
WHERE c.customer_id IS NULL
OR o.order_id IS NULL;
FULL OUTER JOIN vs UNION of LEFT and RIGHT
-- These two approaches produce the same result:
-- Approach 1: FULL OUTER JOIN
SELECT c.first_name, o.order_id
FROM customers c
FULL OUTER JOIN orders o ON c.customer_id = o.customer_id;
-- Approach 2: UNION of LEFT and RIGHT JOINs
SELECT c.first_name, o.order_id
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
UNION
SELECT c.first_name, o.order_id
FROM customers c
RIGHT JOIN orders o ON c.customer_id = o.customer_id;
| Approach | Pros | Cons |
|---|---|---|
FULL OUTER JOIN | Clean, single query | Not supported in MySQL |
LEFT UNION ALL RIGHT | Works everywhere | Slightly more verbose |
Database Support
| Database | FULL OUTER JOIN Support |
|---|---|
| PostgreSQL | Native support |
| SQL Server | Native support |
| Oracle | Native support |
| SQLite | Not supported â use UNION workaround |
| MySQL | Not supported â use UNION workaround |
-- MySQL workaround for FULL OUTER JOIN
SELECT c.customer_id, c.first_name, o.order_id
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
UNION
SELECT c.customer_id, c.first_name, o.order_id
FROM customers c
RIGHT JOIN orders o ON c.customer_id = o.customer_id;
FULL OUTER JOIN with Aggregation
-- Total products per category, including empty categories and orphan products
SELECT
c.category_id,
c.category_name,
COUNT(p.product_id) AS product_count
FROM products p
FULL OUTER JOIN categories c
ON p.category_id = c.category_id
GROUP BY c.category_id, c.category_name
ORDER BY product_count DESC;
Multiple FULL OUTER JOINs
-- Join across three tables, preserving all rows from each
SELECT
c.first_name,
c.last_name,
o.order_date,
p.product_name,
oi.quantity
FROM customers c
FULL OUTER JOIN orders o
ON c.customer_id = o.customer_id
FULL OUTER JOIN order_items oi
ON o.order_id = oi.order_id
FULL OUTER JOIN products p
ON oi.product_id = p.product_id;
Performance Considerations
| Factor | Recommendation |
|---|---|
| Indexes | Create indexes on all join columns |
| Result Size | FULL OUTER JOINs can produce large sets â limit with WHERE early |
| Alternatives | Consider UNION if FULL OUTER JOIN is not supported |
| Statistics | Keep table statistics up to date for the query planner |
| Execution Plan | Use EXPLAIN to verify index usage |
-- Recommended indexes for FULL OUTER JOIN performance
CREATE INDEX idx_orders_customer_id ON orders(customer_id);
CREATE INDEX idx_order_items_order_id ON order_items(order_id);
CREATE INDEX idx_products_category_id ON products(category_id);
Practice Exercises
Exercise 1: Find all customers without orders and all orders without valid customers.
-- Solution
SELECT
c.customer_id,
c.first_name,
o.order_id,
o.order_date
FROM customers c
FULL OUTER JOIN orders o ON c.customer_id = o.customer_id
WHERE c.customer_id IS NULL OR o.order_id IS NULL;
Exercise 2: Build a complete inventory report showing all products and total quantity sold, including products that were never ordered.
-- Solution
SELECT
p.product_id,
p.product_name,
p.category,
SUM(COALESCE(oi.quantity, 0)) AS total_quantity_sold,
SUM(COALESCE(oi.quantity * oi.unit_price, 0)) AS total_revenue
FROM products p
FULL OUTER JOIN order_items oi ON p.product_id = oi.product_id
GROUP BY p.product_id, p.product_name, p.category
ORDER BY total_revenue DESC;
Exercise 3: Create a reconciliation report that shows every employee and every department, including departments with no employees and employees not yet assigned to a department.
-- Solution
SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_id,
d.department_name
FROM employees e
FULL OUTER JOIN departments d ON e.department_id = d.department_id
WHERE e.employee_id IS NULL OR d.department_id IS NULL;
See Also
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions