Back to Chapters
Chapter Progress: 0 %0 / 6 Lessons Read
β€Ήβ€Ί

2. Summarising data: totals and groups

2.1 COUNT, SUM and AVG: one answer from many rows

Highlights & notes

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

    Bookmarks

    Bookmark a lesson to see it here.

      An aggregate function takes many rows and returns one value. Reports, dashboards and data checks are built on these.

      • COUNT(*) β†’ how many rows
      • SUM(amount) β†’ the total
      • AVG(amount) β†’ the average
      • MIN(amount) and MAX(amount) β†’ the smallest and largest

      AS gives the result column a clear name: SUM(amount) AS total. This name is called an alias.

      pandas vs SQL

      • len(df) β†’ COUNT(*)
      • df["amount"].sum() β†’ SUM(amount)
      • df["amount"].mean() β†’ AVG(amount)
      Must Know:

      COUNT(*) counts every row. COUNT(amount) counts only rows where amount is filled in. In orders, COUNT(*) is 8 but COUNT(amount) is 7, because one amount is missing. Comparing the two is a quick check for missing data.

      How it fits together

      1. Many rows
      2. COUNT / SUM / AVG
      3. One row with the answer

      Watch the query run

      SQL

      SELECT COUNT(*) AS orders,       SUM(amount) AS totalFROM ordersWHERE status = 'delivered';

      Rows at this step

      orders | total
      -------+-------
      5      | 120700

      Practice in SQL

      Find the highest and the lowest order amount. Name the columns highest and lowest.

      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
      highest | lowest
      --------+-------
      55000   | 650

      Reference solution
      SELECT MAX(amount) AS highest,
             MIN(amount) AS lowest
      FROM orders;

      Question 1 Β· Easy

      Which function adds up a column?

      Question 2 Β· Medium

      What does AS do in SUM(amount) AS total?

      Question 3 Β· Hard

      The orders table has 8 rows and one missing amount. What is COUNT(amount)?

      Key takeaways

      Aggregates turn many rows into one value. COUNT(*) counts rows; COUNT(column) skips missing values. Use AS to name the result.

      Lesson 7 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
      PreviousCOUNT, SUM and AVG: one answer from many rows
      Save your progress free for 1 month. Start free Β· Log in