Stop 06 · Joining TablesTopics · 3/10

LEFT JOIN

Part of the SQL Roadmap

In short

"Show me every customer and how much they've spent" must include customers who spent nothing. An INNER JOIN silently hides them. LEFT JOIN keeps them, and that's why analysts reach for it so often.

A LEFT JOIN keeps every row from the left table and fills the right table's columns with NULL when there is no match. Use it when missing matches matter, such as customers with no orders.

The jaadu, dressed as a teacher, walks down rows of wooden school desks ticking students in a big register, pausing at one empty desk with a schoolbag still hanging on the chair.

Think of it like this

The class attendance register

A class teacher's register lists every student on the roll, every single day. If Kavya was absent, her name is still there, just with an empty box next to it. LEFT JOIN works like that register: every row from the left table stays, and the right side is left blank (NULL) when there's no match.

Watch it happen

Every customer with their order count

Asha · 3 orders
0
Ravi · 1 order
1
Meena · NULL
2
Farhan · 2 orders
3

1/4customers LEFT JOIN orders. Asha, Ravi and Farhan have orders.

The big ideas

What you need to know

The left side is safe

Every row of the table written first (after FROM) is kept, matched or not.

NULL means no match

Right-table columns are NULL for unmatched rows. That NULL is information, not an error.

Count the right-side key

COUNT(o.order_id) gives 0 for no orders. COUNT(*) would count the NULL row and give 1.

Turn NULL into 0

COALESCE(SUM(o.amount), 0) shows ₹0 instead of an empty cell for customers who never bought.

See it in code

Walk through the code

sql
1SELECT2  c.customer_id,3  c.name,4  c.city,5  COUNT(o.order_id)          AS orders,6  COALESCE(SUM(o.amount), 0) AS total_spent7FROM customers AS c8LEFT JOIN orders AS o9  ON o.customer_id = c.customer_id10GROUP BY c.customer_id, c.name, c.city11ORDER BY total_spent DESC;

1/4customers is on the left, so every customer is kept.

Every customer, including those with zero orders. Works the same in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

COUNT(*) with a LEFT JOIN to count orders per customer.

Do this instead

Customers with no orders still have one joined row, so COUNT(*) says 1. Use COUNT(o.order_id).

Oops

Putting the table you want to keep on the right.

Do this instead

FROM orders LEFT JOIN customers keeps every order, not every customer. Start FROM the table whose rows must all appear.

Oops

Adding WHERE o.status = 'delivered' to a LEFT JOIN.

Do this instead

That removes the NULL rows and turns it back into an inner join. Put right-table filters in ON.

Your mission

Find the quiet customers

~20 min

Quick check

customers has 50 rows and 12 of them have never ordered. How many customers appear in customers LEFT JOIN orders, grouped by customer?

Go deeper

  1. 1List every customer with their order count, including customers with zero orders.
  2. 2Compare the row count with an INNER JOIN on the same tables.
  3. 3Use COALESCE to show 0 instead of NULL for customers without orders.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge