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
    13 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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 NULL after 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

    1. State the business question LEFT JOIN answers and the tables involved.
    2. Read the workflow diagram and map each stage to parser, optimizer, or storage.
    3. Write the smallest correct statement (or design change) for a TechLearningPRO Orders/learners dataset.
    4. Predict the failure mode: wrong cardinality, missing index, or unsafe write — then verify.

    Syntax reference

    Visual workflow / architecture:

    bash
    LEFT JOIN users ──◀── orders
    user 1: ✓ matches → rows
    user 2: ✓ matches → rows
    user 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.amount
    FROM users u
    LEFT JOIN orders o ON o.user_id = u.id;
    users + orders
    u.idu.nameo.user_ido.amount
    1Ana150
    2Leo230
    3Mia——
    joined
    nameamount
    Ana50
    Leo30
    MiaNULL
    ABA (with matching B)

    Mia is kept; her amount is NULL because she has no orders.

    Informative example

    Worked SQL example:

    sql
    SELECT u.name, o.amount
    FROM users u
    LEFT 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.

    5 questions
    1BeginnerQuestionQ1. LEFT JOIN vs INNER JOIN.+

    Answer

    LEFT JOIN keeps every left row.
    2IntermediateQuestionQ2. How to find rows in A with no match in B.+

    Answer

    NULLs fill missing right matches.
    3IntermediateQuestionQ3. Why filtering on B in WHERE can break it.+

    Answer

    Use IS NULL to find missing rows.
    4AdvancedQuestionQ4. <code>COUNT(*)</code> vs <code>COUNT(b.id)</code> on a LEFT JOIN.+

    Answer

    Use IS NULL to find missing rows.
    5AdvancedQuestionQ5. Real example you've used.+

    Answer

    Use 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(*) vs COUNT(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.

    Ready to mark this lesson complete?Track your journey across the entire course.