01 / 21

LEFT JOIN
Preserving Every Row

Learn how to combine tables while keeping all rows from your primary table: even when there is no match on the other side.

SQL

What Is a JOIN?

A JOIN combines rows from two or more tables based on a related column between them. Think of it like matching entries from two spreadsheets using a shared ID.

Left Table
The primary table listed first in your query, written right after FROM.
Right Table
The secondary table you are joining to, listed after the JOIN keyword.
ON Clause
The condition that specifies which columns to match between the two tables.

The Problem LEFT JOIN Solves

Imagine a school with a students table and an enrollments table. Some students have not enrolled in any course yet. A regular INNER JOIN would drop those students from the results entirely.

Visualizing LEFT JOIN

Left Table Right Table Match All rows returned + NULLs for no match

LEFT JOIN Syntax

SELECT columns
FROM left_table
LEFT JOIN right_table
  ON left_table.key \(=\) right_table.key;

How LEFT JOIN Works

students id name 1 Alice 2 Bob 3 Carol enrollments sid course 1 Math 1 Art 3 Science ✕ No match LEFT JOIN Result name course Alice Math Alice Art Bob NULL Carol Science

LEFT JOIN vs INNER JOIN

LEFT JOIN INNER JOIN
Unmatched left rows Kept (with NULLs) Dropped
Matched rows Returned normally Returned normally
Result row count \(\geq\) left table rows Can be more or less
Use case "Show all students" "Show enrolled students"
NULL handling Right columns may be NULL No NULLs from join

Understanding NULL in Results

When a left-table row has no match, every column from the right table is filled with NULL. This is not an error: it is information telling you "no corresponding data exists."

NULL means
Unknown or missing: no row in the right table matched this left row.
Filtering NULLs
Use WHERE column IS NULL to find unmatched rows, or IS NOT NULL to exclude them.

How to Write a LEFT JOIN

1
Identify the primary table: which table's rows must all appear in the result?
2
Write FROM with that table first: it becomes the left table.
3
Add LEFT JOIN + the secondary table: this is the table you want to look up data from.
4
Specify the ON condition: match columns that connect the two tables (usually a foreign key).
5
SELECT your columns: prefix column names with the table name or alias if they are ambiguous.

Example 1: Students and Courses

SELECT s.name, e.course
FROM students s
LEFT JOIN enrollments e
  ON s.id \(=\) e.student_id;
namecourse
AliceMath
AliceArt
BobNULL
CarolScience

Example 2: Finding Unmatched Rows

A powerful pattern: use LEFT JOIN + WHERE ... IS NULL to find rows in the left table that have no match in the right table.

SELECT s.name
FROM students s
LEFT JOIN enrollments e
  ON s.id \(=\) e.student_id
WHERE e.student_id IS NULL;
name
Bob

Example 3: LEFT JOIN with COUNT

Count how many courses each student is enrolled in: including students with zero enrollments.

SELECT s.name,
       COUNT(e.course) AS course_count
FROM students s
LEFT JOIN enrollments e
  ON s.id \(=\) e.student_id
GROUP BY s.name;
namecourse_count
Alice2
Bob0
Carol1

ON vs WHERE with LEFT JOIN

Placing a filter condition in ON versus WHERE produces different results with LEFT JOIN. This is a subtle but critical distinction.

Filter in ON
Filters the right table before joining. Unmatched left rows still appear with NULLs.
Filter in WHERE
Filters after the join. Can eliminate left rows entirely, turning your LEFT JOIN into an INNER JOIN.

Chaining Multiple LEFT JOINs

You can LEFT JOIN more than one table. Each additional join preserves the rows from the left side of that specific join.

SELECT s.name, e.course, g.grade
FROM students s
LEFT JOIN enrollments e
  ON s.id \(=\) e.student_id
LEFT JOIN grades g
  ON e.id \(=\) g.enrollment_id;

Common Mistakes

✕ Wrong table order
Placing the secondary table in FROM and the primary table after LEFT JOIN. The table after FROM is preserved.
✕ WHERE kills LEFT JOIN
Adding WHERE right_table.col \(=\) value filters out NULL rows, silently converting your LEFT JOIN to an INNER JOIN.
✕ Using \(=\) with NULL
Writing WHERE col \(=\) NULL instead of WHERE col IS NULL. The \(=\) operator always returns NULL when comparing to NULL.

JOIN Types Summary

JOIN Type Left Rows Right Rows Unmatched
INNER JOIN Only matched Only matched Dropped
LEFT JOIN All kept Only matched NULL-filled
RIGHT JOIN Only matched All kept NULL-filled
FULL OUTER JOIN All kept All kept NULL-filled

Question 1 of 5

In a LEFT JOIN, what happens to rows in the left table that have no matching row in the right table?

Question 2 of 5

Which query correctly finds customers who have never placed an order?

Question 3 of 5

Given 5 rows in the left table and 3 matching rows in the right table, what is the minimum number of rows a LEFT JOIN can return?

Question 4 of 5

You write: SELECT * FROM A LEFT JOIN B ON A.id \(=\) B.a_id WHERE B.status \(=\) 'active'; What is the problem?

Question 5 of 5

When using LEFT JOIN with COUNT, why should you write COUNT(right_table.column) instead of COUNT(*)?