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

3. Joins: combine tables

3.5 The LEFT JOIN trap: ON or WHERE?

Highlights & notes

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

    Bookmarks

    Bookmark a lesson to see it here.

      Here is a mistake that even experienced people make. You write a LEFT JOIN to keep every customer, then add a filter on the right table in WHERE. Suddenly the customers without a match are gone.

      The reason: WHERE runs after the join. For a customer with no order, o.status is NULL, and NULL = 'pending' is not true. The row is thrown away.

      The fix: put the condition in the ON part. Then it decides which orders are attached, and every customer still stays.

      Must Know:

      A filter on the right table in WHERE quietly turns a LEFT JOIN into an INNER JOIN. Filter the right table in ON, and the left table in WHERE.

      The trap

      We want every customer, with a pending order if there is one.

      SQL

      SELECT c.name, o.productFROM customers AS cLEFT JOIN orders AS o ON o.customer_id = c.idWHERE o.status = 'pending'ORDER BY c.name;

      Rows at this step

      name  | product
      ------+---------
      Kabir | Mouse
      Meera | Keyboard

      Quick check

      Why did Zoya disappear?

      The fix

      The same condition, moved into ON.

      SQL

      SELECT c.name, o.productFROM customers AS cLEFT JOIN orders AS o  ON o.customer_id = c.id AND o.status = 'pending'ORDER BY c.name;

      Rows at this step

      name  | product
      ------+---------
      Asha  | NULL
      Kabir | Mouse
      Meera | Keyboard
      Ravi  | NULL
      Zoya  | NULL

      Quick check

      Where should a filter on the RIGHT table of a LEFT JOIN go if every left row must stay?

      Practice in SQL

      Show every customer name and the id of their cancelled order (NULL if they have none). Sort by name.

      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  | id
      ------+-----
      Asha  | NULL
      Kabir | NULL
      Meera | 104
      Ravi  | NULL
      Zoya  | NULL

      Reference solution
      SELECT c.name, o.id
      FROM customers AS c
      LEFT JOIN orders AS o
        ON o.customer_id = c.id
       AND o.status = 'cancelled'
      ORDER BY c.name;

      Question 1 · Easy

      Which runs first?

      Question 2 · Medium

      LEFT JOIN orders o ON ... WHERE o.status = 'pending' behaves like…

      Question 3 · Hard

      You need all customers from Noida with their delivered orders, if any. Where do the two filters go?

      Key takeaways

      WHERE runs after the join and removes whole rows. To keep every left row in a LEFT JOIN, put conditions on the right table in ON.

      Lesson 17 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
      PreviousThe LEFT JOIN trap: ON or WHERE?
      Save your progress free for 1 month. Start free · Log in