Back to Chapters
Chapter Progress: 0 %0 / 6 Lessons Read
‹›

3. Joins: combine tables

3.3 Find missing rows: LEFT JOIN with IS NULL

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      "Which customers have no orders?" "Which orders point to a customer that does not exist?" Questions about what is missing come up in every data quality check.

      • Step 1: LEFT JOIN so every left row stays
      • Step 2: WHERE right_table.id IS NULL keeps only the rows that found no match

      This pattern is called an anti-join. A condition inside ON limits which right rows can match; a condition in WHERE filters the final rows. Put it in the wrong place and you get a different answer.

      Same question, another way

      SELECT name FROM customers AS c WHERE NOT EXISTS (SELECT 1 FROM orders AS o WHERE o.customer_id = c.id)

      Both forms are common in interviews. Learn one well and recognise the other.

      Must Know:

      = NULL never matches anything, because NULL means "unknown". Always write IS NULL or IS NOT NULL.

      How it fits together

      1. LEFT JOIN
      2. Unmatched rows have NULL
      3. WHERE ... IS NULL keeps them

      Watch the query run

      SQL

      SELECT c.nameFROM customers AS cLEFT JOIN orders AS o  ON o.customer_id = c.idWHERE o.id IS NULL;

      Rows at this step

      name
      ----
      Zoya

      Practice in SQL

      Which customers have no delivered order? Show name, sorted by name.

      Hint: put the status condition inside ON, so it limits which orders can match.

      Tables you can query: customers, orders
      customers
      id | name  | city
      ---+-------+------
      1  | Asha  | Noida
      2  | Ravi  | Delhi
      3  | Meera | Pune
      4  | Kabir | Noida
      5  | Zoya  | NULL
      
      orders
      id  | customer_id | product  | amount | status    | order_date
      ----+-------------+----------+--------+-----------+-----------
      101 | 1           | Laptop   | 55000  | delivered | 2026-01-05
      102 | 2           | Mouse    | 700    | delivered | 2026-01-06
      103 | 1           | Keyboard | 1500   | delivered | 2026-01-09
      104 | 3           | Monitor  | 12000  | cancelled | 2026-01-12
      105 | 2           | Laptop   | 52000  | delivered | 2026-01-15
      106 | 4           | Mouse    | 650    | pending   | 2026-01-18
      107 | 1           | Monitor  | 11500  | delivered | 2026-01-20
      108 | 3           | Keyboard | NULL   | pending   | 2026-01-22
      Expected output
      name
      -----
      Kabir
      Meera
      Zoya

      Reference solution
      SELECT c.name
      FROM customers AS c
      LEFT JOIN orders AS o
        ON o.customer_id = c.id
       AND o.status = 'delivered'
      WHERE o.id IS NULL
      ORDER BY c.name;

      Question 1 · Easy

      How do you test for a missing value?

      Question 2 · Medium

      What does LEFT JOIN ... WHERE o.id IS NULL return?

      Question 3 · Hard

      How many customers have no delivered order?

      Key takeaways

      LEFT JOIN plus WHERE right key IS NULL finds rows with no match. Use IS NULL, never = NULL.

      Lesson 15 of 24

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousFind missing rows: LEFT JOIN with IS NULL
      Save your progress free for 1 month. Start free · Log in