LEFT JOIN

Concepts covered: Outer Joins

An INNER JOIN returns a row only where the ON condition accepts a pair. A row with no partner leaves no trace in the result. That is the right behavior for some questions and a silent bug for others. Take a store with 4 customers and 3 orders.

Ask for every customer with their orders using INNER JOIN and you get 3 rows: Alice twice and Carol once. Bob and Dave have placed no orders, so no pair exists for them and they are gone. A report titled "all customers" now lists 2 of the 4, and nothing in the output says so.

Every left row survives

LEFT JOIN changes one thing. It returns every pair the ON condition accepts, exactly as INNER JOIN does. Then it adds each left row that found no partner, once, with NULL in every column from the right table. The left table is the one named in FROM. The right table is the one named after LEFT JOIN.

SELECT c.name, o.order_id, o.total
FROM customers c
LEFT JOIN orders o
  ON c.cust_id = o.cust_id
customers
cust_idname
C1Alice
C2Bob
C3Carol
C4Dave
orders
order_idcust_idtotal
O1C150
O2C130
O3C375

Join customers to orders with LEFT JOIN on cust_id and the result has 5 rows, each with a reason. Alice pairs with orders O1 and O2, so she appears twice. Carol pairs with O3. Bob and Dave pair with nothing, so each appears once with NULL for order_id and total. Those NULLs mean "no such order exists", which is different from an order whose total is 0.

CustomerOrders that matchRows in resultRight-side columns
Alice22filled from O1 and O2
Bob01all NULL
Carol11filled from O3
Dave01all NULL

You can predict the size of any LEFT JOIN result before running it. The row count equals the number of accepted pairs plus the number of left rows with no pair. Here that is 3 + 2 = 5. An INNER JOIN on the same tables stops at the 3.

Kept at least once

LEFT JOIN guarantees each left row appears at least once. It makes no promise of exactly once. Alice has 2 orders, so she fills 2 rows: a one-to-many relationship multiplies left rows here the same way it does under INNER JOIN. When the report needs one row per customer, group after the join.

Counting through a LEFT JOIN

Grouping a LEFT JOIN result has one trap. Bob's group contains one row, the NULL-padded one, so COUNT(*) reports 1 for a customer with no orders. COUNT(o.order_id) skips NULLs and reports 0, which is the true order count. SUM(o.total) over only NULLs returns NULL, so wrap it in COALESCE when the report should say 0.

SELECT
  c.name,
  COUNT(*) AS row_count,
  COUNT(o.order_id) AS order_count,
  COALESCE(SUM(o.total), 0) AS total_spent
FROM customers c
LEFT JOIN orders o
  ON c.cust_id = o.cust_id
GROUP BY c.name
ORDER BY c.name
customers
cust_idname
C1Alice
C2Bob
C3Carol
C4Dave
orders
order_idcust_idtotal
O1C150
O2C130
O3C375
Result
namerow_countorder_counttotal_spent
Alice2280
Bob100
Carol1175
Dave100

Run it and read Bob's row. row_count is 1, while order_count and total_spent are both 0. On Alice's row the 2 counts agree at 2, and the total is 80. row_count and order_count disagree only for the customers the join had to pad.

A plans table has 3 rows. In subscribers, 4 rows point at Basic and 1 row points at Pro, and the third plan, Plus, has no subscribers. How many rows does this query return?

SELECT
  p.plan_name,
  s.subscriber_id
FROM plans p
LEFT JOIN subscribers s
  ON p.plan_id = s.plan_id

The habit to build is accounting for rows. Before you run a LEFT JOIN, write down how many left rows have partners, how many pairs they form and how many left rows stand alone. If the result's row count differs from your number, something is wrong with the data or with your picture of it, and you want to know which.

About this lesson

This section is part of the Joins: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.