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

3. Joins: combine tables

3.4 JOIN then GROUP BY: totals for each customer

Highlights & notes

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

    Bookmarks

    Bookmark a lesson to see it here.

      The most common report in any company: "total sales for each customer". The names are in customers, the money is in orders. So you join first, then group.

      Think of it as two steps. The join makes one wide row per order, with the customer's name next to it. The GROUP BY then folds those rows into one row per name.

      Must Know:

      To include customers with no orders, use LEFT JOIN and count a column from the orders table, like COUNT(o.id). COUNT(*) would wrongly give them 1.

      Sales for each customer

      Join, then fold.

      SQL

      SELECT c.name,       COUNT(o.id) AS orders,       SUM(o.amount) AS totalFROM customers AS cJOIN orders AS o ON o.customer_id = c.idGROUP BY c.nameORDER BY total DESC;

      Rows at this step

      name  | orders | total
      ------+--------+------
      Asha  | 3      | 68000
      Ravi  | 2      | 52700
      Meera | 2      | 12000
      Kabir | 1      | 650

      Quick check

      Why is Zoya not in the result?

      Include customers with no orders

      LEFT JOIN keeps everyone.

      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 orders DESC, c.name;

      Rows at this step

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

      Quick check

      With the LEFT JOIN, what would COUNT(*) show for Zoya?

      Practice in SQL

      Show the total delivered amount for each customer city. Columns: city, total. Biggest total first.

      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
      city  | total
      ------+------
      Noida | 68000
      Delhi | 52700

      Reference solution
      SELECT c.city, SUM(o.amount) AS total
      FROM customers AS c
      JOIN orders AS o ON o.customer_id = c.id
      WHERE o.status = 'delivered'
      GROUP BY c.city
      ORDER BY total DESC;

      Question 1 · Easy

      To get totals per customer name from two tables, you…

      Question 2 · Medium

      Which join keeps customers that have no orders?

      Question 3 · Hard

      In a LEFT JOIN report, which count gives 0 for a customer with no orders?

      Key takeaways

      Join first, then group. Use LEFT JOIN with COUNT of an orders column to include customers who have no orders.

      Lesson 16 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
      PreviousJOIN then GROUP BY: totals for each customer
      Save your progress free for 1 month. Start free · Log in