Lesson Description
Keep all rows from the left table.
Lesson 2 of 4
Progress: 0/4 exercises solved (0%)
Solve all exercises below to unlock the next lesson.
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 returns every row from the left table. If no matching row exists in the right table, the right-side columns are filled with NULL.
SELECT t1.col, t2.col
FROM left_table t1
LEFT JOIN right_table t2 ON t2.fk = t1.id;
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.
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
| Rows returned | |
|---|---|
| INNER JOIN | Only matching rows |
| LEFT JOIN | All left rows + matching right rows |
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;
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.This lesson is part of Joins. Focus on the core idea in LEFT JOIN, then validate with deliberate practice.
What to master
Common mistakes
High-level strategy
Task ladder
Transparent data checks
Retention loop
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
3 quick questions. One at a time. Instant score at the end.
Return first_name and last_name of customers who have NO orders.
Return doctor first_name, last_name, and the number of appointments they have (alias appointments). Include doctors with zero appointments.
Return every department name and the number of employees in it (employee_count), including departments with zero employees. Order by name.
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.
Lesson 2 of 4
0/4 solved (0%)
Finish this lesson to unlock next.