Mastering MySQL for Data AnalystsLesson 20 of 42
Lesson 20 of 42

Outer Joins: LEFT JOIN, RIGHT JOIN & Emulating FULL OUTER JOIN in MySQL

Overview & Learning Objectives

Unlike an `INNER JOIN` which discards unmatched rows, an `OUTER JOIN` preserves records from one or both tables even when no match exists. A `LEFT JOIN` retains all rows from the left table, padding missing right-table columns with `NULL`—making it the gold standard for finding inactive customers or unsold items. Because MySQL does not natively support `FULL OUTER JOIN`, analysts emulate it by combining `LEFT JOIN` and `RIGHT JOIN` with `UNION`.

  • Preserve all primary entity records using `LEFT JOIN`.
  • Identify orphan or inactive records using anti-join patterns (`WHERE right_key IS NULL`).
  • Emulate `FULL OUTER JOIN` in MySQL using `LEFT JOIN UNION RIGHT JOIN`.

Industry Standard

Standard: Retention analytics at DoorDash and Swiggy use `LEFT JOIN ... WHERE order_id IS NULL` to identify churned users who registered but placed zero orders.

Real-World Example: ScoopCraft Customer Churn & Inactivity Audit

ScoopCraft queries all registered app members alongside their purchase records to identify customers who signed up but never made a purchase.

customer_idcustomer_nameorder_idorder_totalActivity Status
CUST-101Rahul SharmaORD-501₹700.00Active Buyer
CUST-102Kavita ReddyNULLNULLInactive / Zero Orders (Anti-Join Match)
CUST-103Ananya SenORD-502₹280.00Active Buyer

Syntax & Core SQL Implementation

-- LEFT JOIN: Retain all customers, even those with 0 orders
SELECT 
    c.customer_id,
    c.customer_name,
    c.phone,
    COUNT(o.order_id) AS total_orders,
    IFNULL(SUM(o.order_total), 0.00) AS total_spend
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.customer_name, c.phone;

-- Anti-Join: Find customers who NEVER placed an order
SELECT c.customer_id, c.customer_name, c.email
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;

-- Emulating FULL OUTER JOIN in MySQL using UNION
SELECT c.customer_id, o.order_id
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
UNION
SELECT c.customer_id, o.order_id
FROM customers c
RIGHT JOIN orders o ON c.customer_id = o.customer_id;

Parameters & Arguments Breakdown

  • LEFT JOIN: Preserves all records from left table; populates right table columns with NULL if no match exists.
  • RIGHT JOIN: Preserves all records from right table; populates left table columns with NULL if no match exists.
  • WHERE right_key IS NULL: Anti-join pattern that filters for rows that had no matching partner.

Sample Walkthrough (Q&A)

Question: Why does adding a WHERE condition on a right-table column inadvertently convert a LEFT JOIN into an INNER JOIN?

SQL Pattern: LEFT JOIN orders o ON c.id = o.c_id WHERE o.order_date > "2026-01-01"

Result: Excludes customers with 0 orders because NULL > "2026-01-01" evaluates to UNKNOWN

Explanation: To preserve customers with 0 orders, filter conditions on the joined table must be placed in the ON clause, not the WHERE clause.

Common Mistakes & Limitations

  • Putting right-table filter criteria in the `WHERE` clause instead of the `ON` clause, unintentionally eliminating unmatched left rows.
  • Assuming MySQL natively supports the `FULL OUTER JOIN` keyword; running it in MySQL raises a syntax error.

Keyboard Shortcuts (MySQL Workbench)

ActionWindowsMac
Execute QueryCtrl + EnterCmd + Enter

Pro Tips & Modern Best Practices

  • Always prefer `LEFT JOIN` over `RIGHT JOIN`; reading queries left-to-right matches natural mental models.
  • Use `LEFT JOIN ... WHERE right_table.id IS NULL` as a high-performance alternative to `NOT IN` subqueries.