What is the exact difference between INNER JOIN and OUTER JOIN in SQL? | A1Lab@HumairaM to SQLEnglish • 2 days ago

What is the exact difference between INNER JOIN and OUTER JOIN in SQL?

message-square
0

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 in customers

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 because 99 is not in customers.

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 because orders is the right (preserved-only-on-match) table in a LEFT 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 NULL order columns, and order 103 with NULL customer columns. (Note: MySQL does not natively support FULL OUTER JOIN directly; you simulate it with LEFT 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

  1. Use INNER JOIN when you only want records that exist in both tables.
  2. Use LEFT JOIN when 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 using WHERE o.order_id IS NULL).
  3. Use FULL JOIN when you want to reconcile two datasets side by side and see unmatched records from both sides.