LEFT JOIN
LEFT JOIN returns all rows from the left table, plus matching rows from the right (NULL where none match).
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.
Learning objective: Apply LEFT JOIN correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.
Treat every example as production-shaped SQL: explicit column lists, deterministic predicates, and indexes that match the access path. Prefer PostgreSQL dialect notes when dialects diverge; call out MySQL differences when they matter.
Purpose of this lesson
Apply LEFT JOIN correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.
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.
Step-by-step explanation
- State the business question LEFT JOIN answers and the tables involved.
- Read the workflow diagram and map each stage to parser, optimizer, or storage.
- Write the smallest correct statement (or design change) for a TechLearningPRO Orders/learners dataset.
- Predict the failure mode: wrong cardinality, missing index, or unsafe write — then verify.
Syntax reference
Visual workflow / architecture:
LEFT JOIN users ──◀── ordersuser 1: ✓ matches → rowsuser 2: ✓ matches → rowsuser 3: ✗ no orders → row with NULLs
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.
SELECT u.name, o.amountFROM users uLEFT JOIN orders o ON o.user_id = u.id;
| u.id | u.name | o.user_id | o.amount |
|---|---|---|---|
| 1 | Ana | 1 | 50 |
| 2 | Leo | 2 | 30 |
| 3 | Mia | — | — |
| name | amount |
|---|---|
| Ana | 50 |
| Leo | 30 |
| Mia | NULL |
Mia is kept; her amount is NULL because she has no orders.
Informative example
Worked SQL example:
SELECT u.name, o.amountFROM users uLEFT JOIN orders o ON o.user_id = u.id;
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.
Security implications
SQL is a trust boundary. Application code must never concatenate untrusted input into statements.
- Use bind parameters / prepared statements — never string-built SQL from request bodies.
- Least-privilege DB roles: app users should not own DDL in production.
- Avoid returning raw DB errors to clients; log server-side with a correlation id.
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).
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionQ1. LEFT JOIN vs INNER JOIN.+
Answer
2IntermediateQuestionQ2. How to find rows in A with no match in B.+
Answer
3IntermediateQuestionQ3. Why filtering on B in WHERE can break it.+
Answer
IS NULL to find missing rows.4AdvancedQuestionQ4. <code>COUNT(*)</code> vs <code>COUNT(b.id)</code> on a LEFT JOIN.+
Answer
IS NULL to find missing rows.5AdvancedQuestionQ5. Real example you've used.+
Answer
IS NULL to find missing rows.Hands-on exercise
Hands-on + interview drill:
- Implement the smallest correct example for LEFT JOIN on an Orders-like schema.
- 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.
Summary
LEFT JOIN is a production SQL skill: state the question, write the precise statement, and prove it with an execution plan or constraint — not with lucky result sets.