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

3. Joins: combine tables

3.2 LEFT JOIN: keep every row from the first table

Highlights & notes

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

    Bookmarks

    Bookmark a lesson to see it here.

      A LEFT JOIN keeps every row from the left table (the one after FROM), even when there is no match. The columns from the right table are then filled with NULL, which means "no value".

      • INNER JOIN → only customers who have orders
      • LEFT JOIN → all customers; order columns are NULL for those with none

      Use it whenever losing rows would be wrong: every customer in a report, every day in a calendar, every product in a catalogue.

      pandas vs SQL

      customers.merge(orders, how="left", ...) → FROM customers LEFT JOIN orders ON ...

      Must Know:

      Check row counts after every join. A left join never loses left rows, but it repeats a left row once for every match. Asha has 3 orders, so she appears 3 times. This is the most common cause of doubled totals.

      How it fits together

      1. All customers
      2. LEFT JOIN orders
      3. Everyone kept, NULL if no order

      Watch the query run

      SQL

      SELECT c.name,       COUNT(o.id) AS ordersFROM customers AS cLEFT JOIN orders AS o  ON o.customer_id = c.idGROUP BY c.nameORDER BY c.name;

      Rows at this step

      name  | orders
      ------+-------
      Asha  | 3
      Kabir | 1
      Meera | 2
      Ravi  | 2
      Zoya  | 0

      Practice in SQL

      Show every customer's name and the total amount of their orders as total. Customers with no orders must still appear. 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  | total
      ------+------
      Asha  | 68000
      Kabir | 650
      Meera | 12000
      Ravi  | 52700
      Zoya  | NULL

      Reference solution
      SELECT c.name,
             SUM(o.amount) AS total
      FROM customers AS c
      LEFT JOIN orders AS o
        ON o.customer_id = c.id
      GROUP BY c.name
      ORDER BY c.name;

      Question 1 · Easy

      In FROM customers LEFT JOIN orders, which table keeps all its rows?

      Question 2 · Medium

      A customer has no orders. What is in the order columns after a LEFT JOIN?

      Question 3 · Hard

      customers LEFT JOIN orders returns 9 rows from 5 customers. Why?

      Key takeaways

      LEFT JOIN keeps every left row and fills NULL where nothing matches. Rows repeat for multiple matches, so check counts.

      Lesson 14 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
      PreviousLEFT JOIN: keep every row from the first table
      Save your progress free for 1 month. Start free · Log in