MySQL LEFT JOIN
MySQL LEFT JOIN: Preserving Every Left Table Row
The MySQL LEFT JOIN (or LEFT OUTER JOIN) returns every row from the left table along with matching rows from the right table. When no match exists in the right table, MySQL still keeps the left row and fills all right-table columns with NULL.
Basic Syntax
SELECT l.column1, r.column2
FROM left_table AS l
LEFT JOIN right_table AS r
ON l.id = r.left_id;
Practical SQL Examples
The first query lists every customer in demo_customers with their order count—even customers with zero orders. The second uses an anti-join pattern (WHERE o.id IS NULL) to find inactive signups who never purchased:
-- Include customers who have 0 orders
SELECT
c.id,
c.first_name,
COUNT(o.id) AS total_orders,
COALESCE(SUM(o.total_amount), 0.00) AS total_spent
FROM demo_customers AS c
LEFT JOIN demo_orders AS o
ON o.customer_id = c.id
AND o.status = 'paid'
GROUP BY c.id, c.first_name
ORDER BY total_orders ASC;
-- Find customers with zero orders
SELECT c.id, c.first_name, c.email
FROM demo_customers AS c
LEFT JOIN demo_orders AS o
ON o.customer_id = c.id
WHERE o.id IS NULL;
| id | first_name | total_orders | total_spent |
|---|---|---|---|
| 3 | Alice | 0 | 0.00 |
| 2 | John | 2 | 290.50 |
| 1 | Jane | 4 | 645.00 |
Common Mistakes and Best Practices
- Never Filter Right-Table Columns in
WHERE: WritingWHERE o.status = 'paid'after aLEFT JOINdiscards every unmatched row whereo.statusisNULL, silently turning yourLEFT JOINinto anINNER JOIN! Put right-table conditions inside theONclause instead. - Count
COUNT(o.id), NotCOUNT(*):COUNT(*)counts theNULL-padded row as1even when a customer has zero orders. Always count the non-null right keyCOUNT(o.id).
Frequently Asked Questions
Are LEFT JOIN and LEFT OUTER JOIN the same in MySQL?
Yes, OUTER is optional in SQL syntax; both keywords execute the exact same join behavior.
How do I find rows in the left table with no match in the right table?
Join on the key and add WHERE right_table.id IS NULL, or use WHERE NOT EXISTS (...).
Why did my LEFT JOIN drop rows when I filtered the right table?
Because WHERE right_table.col = 'value' rejects NULL, filtering out unmatched left rows; move that filter into the ON condition.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.