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

2. Summarising data: totals and groups

2.5 Group by two columns and sort by the total

Highlights & notes

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

    Bookmarks

    Bookmark a lesson to see it here.

      You can sort groups by the number you calculated. "Which status has the most money?" is a GROUP BY followed by ORDER BY total DESC.

      You can also group by two columns. Then each different pair becomes one group: one row for (customer 1, delivered), another for (customer 2, delivered), and so on.

      Must Know:

      Every column in SELECT must be either inside GROUP BY or inside an aggregate like SUM. Otherwise the database does not know which value to show for the group.

      Totals, biggest first

      One row per status, sorted by money.

      SQL

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

      Rows at this step

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

      Quick check

      In which order do these parts run?

      Two columns, one group per pair

      Customer and status together.

      SQL

      SELECT customer_id, status, COUNT(*) AS ordersFROM ordersGROUP BY customer_id, statusORDER BY customer_id, status;

      Rows at this step

      customer_id | status    | orders
      ------------+-----------+-------
      1           | delivered | 3
      2           | delivered | 2
      3           | cancelled | 1
      3           | pending   | 1
      4           | pending   | 1

      Quick check

      GROUP BY city, product on rows (Noida, Mouse), (Noida, Mouse), (Noida, Laptop) gives how many groups?

      Practice in SQL

      For each product show how many orders it has (orders) and its average amount (avg_amount). Put the highest average 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
      product  | orders | avg_amount
      ---------+--------+-----------
      Laptop   | 2      | 53500
      Monitor  | 2      | 11750
      Keyboard | 2      | 1500
      Mouse    | 2      | 675

      Reference solution
      SELECT product, COUNT(*) AS orders, AVG(amount) AS avg_amount
      FROM orders
      GROUP BY product
      ORDER BY avg_amount DESC;

      Question 1 · Easy

      Which clause sorts the groups?

      Question 2 · Medium

      GROUP BY customer_id, status makes one group for each…

      Question 3 · Hard

      SELECT status, product, SUM(amount) FROM orders GROUP BY status. What is wrong?

      Key takeaways

      Sort groups with ORDER BY on the aggregate. Grouping by two columns makes one group per different pair. Every selected column must be grouped or aggregated.

      Lesson 11 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 two columns and sort by the total
      Save your progress free for 1 month. Start free · Log in