Nishaglobal Education logo

LEFT JOIN: keeping rows without a match

Keep every row from one table with SQL LEFT JOIN, find customers with no orders and products never sold, and count correctly so that zero shows as 0.

Lesson 10 of 1225 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Explain the difference between INNER JOIN and LEFT JOIN
  • Find rows with no match using LEFT JOIN and IS NULL
  • Count related rows so that zero appears as 0
Outline
  1. What you'll learn and why it matters
  2. Keeping every row from the left table
  3. Finding rows with no match
  4. Counting with zero
  5. Practice
  6. Common mistakes
  7. Recap
  8. Quiz
  9. Mark lesson complete

What you'll learn and why it matters

Some of the most valuable business questions are about what is missing: customers who signed up but never bought anything, products nobody orders, students who have not submitted work. An inner join can never show these, because it drops rows without a match. A LEFT JOIN keeps them.

Keeping every row from the left table

LEFT JOIN keeps every row from the first (left) table. Where there is a matching row in the right table, its columns are filled in; where there is none, they are NULL.

SQL
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE c.customer_id IN (1, 7, 10)
ORDER BY c.customer_id, o.order_id;

Result: 5 rows

Query result
nameorder_id
Asha Patel1
Asha Patel2
Asha Patel12
Sara LopezNULL
Kofi MensahNULL
customers (left)customer_idname1Asha7Sara10Kofiorders (right)order_idcustomer_id1121121LEFT JOIN ... ON customer_idresult: every customer keptnameorder_idAsha1Asha2Asha12SaraNULLKofiNULLNo order → order columns are NULL (shaded rows)
A LEFT JOIN keeps every customer. Customers with orders get one row per order; customers without orders get one row with NULL in the order columns.
JoinKeepsSara (no orders)
JOIN (inner)Only rows that match in both tablesNot shown
LEFT JOINEvery row from the left table, matched or notShown once, with NULL order_id

Finding rows with no match

Combine a LEFT JOIN with IS NULL on the right table's key to keep only the unmatched rows. Who has never ordered?

SQL
SELECT c.name, c.email
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;

Result: 2 rows

Query result
nameemail
Sara LopezNULL
Kofi Mensahkofi@example.com

Which products have never been ordered?

SQL
SELECT p.name
FROM products AS p
LEFT JOIN orders AS o ON o.product_id = p.product_id
WHERE o.order_id IS NULL;

Result: 1 row

Query result
name
Coffee Mug

This “left join, then IS NULL” pattern is worth memorising. You will use it all the time.

Counting with zero

To count orders per customer including customers with none, left join and count a column from the right table:

SQL
SELECT c.name, COUNT(o.order_id) AS orders
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
ORDER BY orders DESC, c.name;

Result: 10 rows

Query result
nameorders
Asha Patel3
Arjun Singh2
Fatima Khan2
John Smith2
Meera Nair2
Ravi Kumar2
Emma Brown1
Li Wei1
Kofi Mensah0
Sara Lopez0
Wrong: COUNT(*) shows 1 for customers with no orders
SELECT c.name, COUNT(*) AS orders
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE c.customer_id IN (7, 10)
GROUP BY c.customer_id, c.name;

Result: 2 rows

Query result
nameorders
Sara Lopez1
Kofi Mensah1

Practice

Guided exercise

Orders per category, including empty ones

Show each product category and how many orders it has had, using a LEFT JOIN from products so that every category appears. Sort by orders, highest first, then by category.

  1. Start FROM products AS p and LEFT JOIN orders AS o ON o.product_id = p.product_id.
  2. Group by p.category.
  3. Count o.order_id, not *.
Show the solution
Solution
SELECT p.category, COUNT(o.order_id) AS orders
FROM products AS p
LEFT JOIN orders AS o ON o.product_id = p.product_id
GROUP BY p.category
ORDER BY orders DESC, p.category;

Result: 4 rows

Query result
categoryorders
Books5
Electronics4
Stationery4
Home2

How to get there: Home has only 2 orders, both for the Water Bottle; the Coffee Mug contributes a joined row with NULL, which COUNT(o.order_id) correctly ignores.

Your turn

Win-back list

Marketing wants to email customers who have never ordered, but only those with an email address. List their name, email and join date.

Show the solution
Solution
SELECT c.name, c.email, c.joined_on
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL AND c.email IS NOT NULL;

Result: 1 row

Query result
nameemailjoined_on
Kofi Mensahkofi@example.com2025-09-14

How to get there: The LEFT JOIN plus o.order_id IS NULL finds non-buyers; c.email IS NOT NULL removes Sara, who cannot be emailed.

Common mistakes

Tables in the wrong order

You see: The rows you wanted to keep still disappear

Fix: Put the table whose rows must all appear in FROM, then LEFT JOIN the other.

Using COUNT(*) after a LEFT JOIN

You see: Customers with no orders show 1 instead of 0

Fix: Count a column from the right table: COUNT(o.order_id).

Filtering the right table in WHERE, e.g. WHERE o.status = 'delivered'

You see: The unmatched rows vanish again (their status is NULL)

Fix: Put such conditions in the ON clause: ON o.customer_id = c.customer_id AND o.status = 'delivered'.

Checking IS NULL on a column that can be NULL anyway

You see: Wrong rows reported as missing

Fix: Test the right table's key column, such as o.order_id.

Recap

  • LEFT JOIN keeps every row from the left (FROM) table; unmatched right-side columns are NULL.
  • LEFT JOIN plus WHERE right.key IS NULL finds rows with no match.
  • Count a right-table column, not *, so that zero shows as 0.
  • Conditions on the right table usually belong in ON, not WHERE.

Key terms

LEFT JOIN
A join that keeps every row from the left table, filling unmatched columns with NULL.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. Which query lists customers who have never ordered?

  2. 2. After a LEFT JOIN from customers to orders, why does COUNT(*) show 1 for a customer with no orders?

  3. 3. In `FROM products LEFT JOIN orders`, which table's rows are all kept?

  4. 4. How many rows does a LEFT JOIN from customers to orders give for a customer with 3 orders?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.