An INNER JOIN returns only rows where matching join keys exist in both tables, dropping unmatched rows from either side. An OUTER JOIN (LEFT OUTER JOIN, RIGHT OUTER JOIN, or FULL OUTER JOIN) returns all matching rows plus unmatched rows from one or both tables, filling missing columns from the non-matching side with NULL.
1. Core Behavioral Difference
| Aspect | INNER JOIN |
OUTER JOIN (LEFT, RIGHT, FULL) |
|---|---|---|
| Matching rows | Included when join condition is true | Included when join condition is true |
| Unmatched rows on left table | Excluded | Included with LEFT JOIN and FULL JOIN (NULLs on right columns) |
| Unmatched rows on right table | Excluded | Included with RIGHT JOIN and FULL JOIN (NULLs on left columns) |
| Missing side values | Never occurs (both sides matched) | Represented as NULL |
2. Sample Tables
Suppose we have customers and orders:
- customers:
(1, 'Amina')(2, 'Bilal')(3, 'Sara')— has no orders
- orders:
(101, 1, 500)— for Amina(102, 1, 250)— for Amina(103, 99, 120)— for customer_id 99, not incustomers
3. SQL Examples
INNER JOIN (Only Customers With Matching Orders)
SELECT
c.customer_id,
c.customer_name,
o.order_id,
o.amount
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id;
- Returns 2 rows (Amina’s two orders).
- Sara (
customer_id = 3) is excluded because she has no orders. - Order
103(customer_id = 99) is excluded because99is not incustomers.
LEFT OUTER JOIN (All Customers, Plus Matching Orders if Any)
SELECT
c.customer_id,
c.customer_name,
o.order_id,
o.amount
FROM customers AS c
LEFT OUTER JOIN orders AS o
ON c.customer_id = o.customer_id;
- Returns 3 rows: Amina’s two orders plus
(3, 'Sara', NULL, NULL). - Order
103(customer_id = 99) still does not appear becauseordersis the right (preserved-only-on-match) table in aLEFT JOIN.
RIGHT OUTER JOIN (All Orders, Plus Matching Customers if Any)
SELECT
c.customer_id,
c.customer_name,
o.order_id,
o.amount
FROM customers AS c
RIGHT OUTER JOIN orders AS o
ON c.customer_id = o.customer_id;
- Returns 3 rows: Amina’s two orders plus
(NULL, NULL, 103, 120). - Sara is excluded because she has no matching row in
orders.
FULL OUTER JOIN (All Rows From Both Sides)
SELECT
c.customer_id,
c.customer_name,
o.order_id,
o.amount
FROM customers AS c
FULL OUTER JOIN orders AS o
ON c.customer_id = o.customer_id;
- Returns 4 rows: Amina’s two orders, Sara with
NULLorder columns, and order103withNULLcustomer columns. (Note: MySQL does not natively supportFULL OUTER JOINdirectly; you simulate it withLEFT JOIN+UNION+RIGHT JOIN.)
4. Common Pitfall: Filtering the Right Table in WHERE vs. ON
In a LEFT JOIN, putting a condition on the right table inside WHERE can turn the outer join into an effective INNER JOIN because NULL values fail equality checks:
-- Turns LEFT JOIN into an effective INNER JOIN for non-matching customers:
SELECT c.customer_name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
WHERE o.amount > 200;
-- Preserves unmatched customers while filtering right-table rows:
SELECT c.customer_name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
AND o.amount > 200;
Quick Decision Rule
- Use
INNER JOINwhen you only want records that exist in both tables. - Use
LEFT JOINwhen you want every row from the primary/left table, even if related rows on the right do not exist (for example, finding customers with zero orders usingWHERE o.order_id IS NULL). - Use
FULL JOINwhen you want to reconcile two datasets side by side and see unmatched records from both sides.
You must log in or register to comment.
