Skip to content
KALANBLON
Show contents

Chapter 2 of 3 · 15 min

Counting and grouping

By the end of this chapter, you'll be able to

  • Count rows with COUNT
  • Compute a sum and an average with SUM and AVG
  • Group rows with GROUP BY
  • Filter groups with HAVING

Aggregate functions

An aggregate function summarises several rows into a single value: COUNT(*) counts the rows, SUM(column) adds up, AVG(column) averages, MIN and MAX give the extremes. AS gives the computed column a readable name.

SELECT COUNT(*) AS nb_sales, SUM(quantity) AS units, MAX(price) AS max_price
FROM sales;
nb_sales | units | max_price
10 | 215 | 4

GROUP BY: one result per group

GROUP BY city sorts the rows into groups, one per city, then applies the aggregation to each group:

ORDER BY total DESC sorts the cities from biggest to smallest.

Step 4 of 4
Text description of the animation
  1. We want the total number of units sold per city.
  2. GROUP BY puts each sale into its city's group.
  3. SUM(quantity) is computed separately for each group: one result row per city.
  4. ORDER BY total DESC sorts the cities from biggest to smallest.
SELECT city, SUM(quantity) AS total
FROM sales
GROUP BY city
ORDER BY total DESC;
city | total
Dakar | 90
Hanoi | 85
Lima | 40

You can aggregate a calculation. A sale's revenue is the quantity multiplied by the price; ROUND(x, 2) rounds to two decimal places:

SELECT category, ROUND(SUM(quantity * price), 2) AS revenue
FROM sales
GROUP BY category
ORDER BY revenue DESC;
category | revenue
cereal | 174
drink | 155
fruit | 58.2

HAVING: filtering groups

WHERE filters rows before grouping. HAVING filters groups after the calculation:

SELECT product, SUM(quantity) AS total
FROM sales
GROUP BY product
HAVING total >= 30
ORDER BY total DESC;
product | total
Rice | 90
Mango | 38

Your turn

Mini-lab: the average basketSQLite

Result

Click “Run”: the SQL engine (about 1 MB) will load in your browser the first time.

A fresh SQLite database is created on every run, in your browser · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Solved exercise: the categories that sell

Display, for each category, the number of units sold, keeping only the categories that sold more than 50 units.

Hint 1
Group by category and add up quantity.
Hint 2
The condition is about a group's total: that's a job for HAVING.
Show the solution and common mistakes
SELECT category, SUM(quantity) AS units
FROM sales
GROUP BY category
HAVING units > 50
ORDER BY units DESC;
category | units
cereal | 110
fruit | 55

Drinks (50 units) aren't displayed: 50 isn't strictly greater than 50.

Common mistakes:

  • Putting the condition in WHERE: misuse of aggregate.
  • Forgetting the GROUP BY: SQL then computes a single total for the whole table.

Summary

  1. 1COUNT, SUM, AVG, MIN and MAX summarise several rows into one value.
  2. 2GROUP BY computes an aggregation separately for each group.
  3. 3WHERE filters rows before grouping, HAVING filters groups after.
  4. 4AS gives a computed column a readable name.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.