SQL Tutorial 0/85 lessons ~6 min read Lesson 37
LEFT JOIN
LEFT JOIN returns all rows from the left table, plus matching rows from the right (NULL where none match).
Course progress0%
Focus
7 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
LEFT JOIN returns all rows from the left table, plus matching rows from the right (NULL where none match). Use it whenever you want every row from one side, regardless of matches.
Understanding the topic
Core concepts to understand:
FROM a LEFT JOIN b ON ....- All A rows kept; B columns NULL when no match.
- Find missing rows:
WHERE b.id IS NULLafter LEFT JOIN. - Common for reports — show users even if they have zero orders.
- Index the right-side join column for speed.
Syntax reference
Visual workflow / architecture:
bash
LEFT JOIN users ──◀── ordersuser 1: ✓ matches → rowsuser 2: ✓ matches → rowsuser 3: ✗ no orders → row with NULLs
Syntax Pattern
SELECT <cols>
FROM <a> [AS a]
LEFT JOIN <b> [AS b]
ON <a.key> = <b.key>;LEFT JOINKeeps every row from A; B columns are NULL when no match.IS NULLFilter b.id IS NULL to find A rows missing in B.
Live Example
sql
SELECT u.name, o.amountFROM users uLEFT JOIN orders o ON o.user_id = u.id;
users + orders
| u.id | u.name | o.user_id | o.amount |
|---|---|---|---|
| 1 | Ana | 1 | 50 |
| 2 | Leo | 2 | 30 |
| 3 | Mia | — | — |
runs
joined
| name | amount |
|---|---|
| Ana | 50 |
| Leo | 30 |
| Mia | NULL |
Mia is kept; her amount is NULL because she has no orders.
Real-world use
Dashboard 'all users + their order count (0 if none)' is the canonical LEFT JOIN. Same for products with no sales, devices with no events.
Best practices
- Use LEFT JOIN when the left table is the source of truth.
- Be careful — adding a WHERE on right-side columns can silently turn it into INNER.
- Index the right-side join column.
Common mistakes
- Filtering right-side columns in WHERE breaks the LEFT JOIN semantic.
- COUNT(*) over LEFT JOIN counts NULL rows — use COUNT(b.id).
Hands-on exercise
Interview preparation — practice these questions:
- Q1. LEFT JOIN vs INNER JOIN.
- Q2. How to find rows in A with no match in B.
- Q3. Why filtering on B in WHERE can break it.
- Q4.
COUNT(*)vsCOUNT(b.id)on a LEFT JOIN. - Q5. Real example you've used.
Ready to mark this lesson complete?Track your journey across the entire course.