Lesson 2 of 4

Progress: 0/4 exercises solved (0%)

← Previous

Solve all exercises below to unlock the next lesson.

LEFT JOIN

Lesson Description

Keep all rows from the left table.

Easy Project

Mini project: Build a people-and-departments report that lists employee names, department names, and validates row counts to prove your join is correct.

JOIN Visual

employees

departments

Match keys first, then verify row counts after the join.

LEFT JOIN — keep all rows from the left table

LEFT JOIN returns every row from the left table. If no matching row exists in the right table, the right-side columns are filled with NULL.

Syntax

SELECT t1.col, t2.col
FROM left_table  t1
LEFT JOIN right_table t2 ON t2.fk = t1.id;

Example: customers and their orders (including those with no orders)

SELECT
  c.first_name,
  c.last_name,
  o.id AS order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;

Customers with no orders appear once with order_id = NULL.

Finding unmatched rows

Filter for NULL on the right side to find rows with no match — a common pattern for finding gaps:

SELECT c.first_name, c.last_name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;   -- customers who never ordered

LEFT JOIN vs INNER JOIN

Rows returned
INNER JOINOnly matching rows
LEFT JOINAll left rows + matching right rows

Aggregating after LEFT JOIN

COUNT(t2.id) returns 0 for unmatched rows (because t2.id is NULL), while COUNT(*) would return 1:

SELECT d.name, COUNT(e.id) AS headcount
FROM departments d
LEFT JOIN employees e ON e.department_id = d.id
GROUP BY d.id, d.name;

Tips

  • LEFT JOIN is the most commonly used outer join.
  • RIGHT JOIN is the mirror image — all right rows kept. Usually rewritten as a LEFT JOIN by swapping table order.

Learning context

This lesson is part of Joins. Focus on the core idea in LEFT JOIN, then validate with deliberate practice.

What to master

  • Choose the base table (one row = what?)
  • Match join keys on true primary/foreign pairs
  • Validate row count after every join

Common mistakes

  • Joining many-to-many without pre-aggregation
  • Using DISTINCT to hide a bad join
  • Forgetting LEFT JOIN when keeping base rows

High-level strategy

  • Start with INNER JOIN to verify key matches
  • Switch to LEFT JOIN only when preserving base rows matters
  • Use COUNT(*) and COUNT(DISTINCT key) as join safety checks

Task ladder

  1. Baseline query: get a correct first output.
  2. Add one complication: edge case, join, filter, or ranking rule.
  3. Re-run and refine until output is stable and explainable.

Transparent data checks

  • Check row count before and after each major clause.
  • Validate one manual sample row for correctness.
  • Confirm final ordering and column naming for trust.

Retention loop

  • Run the solution once.
  • Rewrite from memory with one variation.
  • Explain the logic in plain English in one sentence.

Logical reasoning for commands

JOIN ... ON

Why: Connects rows only when keys truly match.

Memory cue: No key match = no trusted relationship.

LEFT JOIN

Why: Keeps base table rows even when lookup data is missing.

Memory cue: Left keeps the story intact.

COUNT(DISTINCT key)

Why: Confirms joins did not multiply business entities.

Memory cue: Distinct key count should stay believable.

Concept check

Quick check: LEFT JOIN

3 quick questions. One at a time. Instant score at the end.

Exercise 1

Customers with no orders

Commerce Operations Intelligencemedium

Return first_name and last_name of customers who have NO orders.

Run a query to see results here.
Exercise 2

Doctors and their appointment count

Clinical Care Performancemedium

Return doctor first_name, last_name, and the number of appointments they have (alias appointments). Include doctors with zero appointments.

Run a query to see results here.
Exercise 3

All departments with employee count

Enterprise Workforce Analyticseasy

Return every department name and the number of employees in it (employee_count), including departments with zero employees. Order by name.

Run a query to see results here.
Exercise 4

Products with total quantity sold

Commerce Operations Intelligenceeasy

From the store sandbox, return each product name and total_sold (sum of quantity from order_items). Include products that have never been sold (show 0). Order by total_sold descending.

Run a query to see results here.

Lesson 2 of 4

0/4 solved (0%)

← Previous

Finish this lesson to unlock next.