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

2. Summarising data: totals and groups

2.2 GROUP BY: one result row per group

Highlights & notes

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

    Bookmarks

    Bookmark a lesson to see it here.

      GROUP BY puts rows with the same value into one bucket, then runs the aggregate once per bucket. It answers every "per" question: sales per city, orders per customer, errors per day.

      • GROUP BY status → one row for each status
      • GROUP BY city, status → one row for each city and status pair
      • The aggregate (COUNT, SUM...) is worked out inside each group

      With grouping, the run order becomes FROM → WHERE → GROUP BY → SELECT → ORDER BY.

      pandas vs SQL

      df.groupby("status")["amount"].sum()

      is the same idea as

      SELECT status, SUM(amount) FROM orders GROUP BY status

      Must Know:

      Every column in SELECT must either be in GROUP BY or be inside an aggregate. SELECT status, product, COUNT(*) ... GROUP BY status is wrong: one status has many products, so the database cannot pick one.

      How it fits together

      1. Rows
      2. Buckets by status
      3. One summary row per bucket

      Watch the query run

      SQL

      SELECT status,       COUNT(*) AS orders,       SUM(amount) AS totalFROM ordersGROUP BY statusORDER BY status;

      Rows at this step

      status    | orders | total
      ----------+--------+-------
      cancelled | 1      | 12000
      delivered | 5      | 120700
      pending   | 2      | 650

      Practice in SQL

      For each customer_id, show the total amount of their orders as total. Sort by customer_id.

      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
      customer_id | total
      ------------+------
      1           | 68000
      2           | 52700
      3           | 12000
      4           | 650

      Reference solution
      SELECT customer_id,
             SUM(amount) AS total
      FROM orders
      GROUP BY customer_id
      ORDER BY customer_id;

      Question 1 · Easy

      What does GROUP BY city return?

      Question 2 · Medium

      How many rows does SELECT product, COUNT(*) FROM orders GROUP BY product return?

      Question 3 · Hard

      Which query is wrong?

      Key takeaways

      GROUP BY makes one row per group and runs the aggregate inside each group. Every selected column must be grouped or aggregated.

      Lesson 8 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
      PreviousGROUP BY: one result row per group
      Save your progress free for 1 month. Start free · Log in