By Kevin McAleer, 6 Minutes
Page last updated November 24, 2026

By the end of this lesson you will be able to summarise rows with aggregate functions, group them with GROUP BY, and filter whole groups with HAVING.
A robot taking a reading every second makes thousands of rows. You rarely want to read them all. You want the average, the closest object, how many readings were taken. These summaries are called aggregates.
We need some data to summarise. Add a readings table, with a few distance readings in centimetres (and a few acceleration readings in g from the MPU6050):
CREATE TABLE readings (
reading_id INTEGER PRIMARY KEY,
sensor_id INTEGER NOT NULL REFERENCES sensors (sensor_id),
taken_at TEXT NOT NULL,
value REAL NOT NULL
);
INSERT INTO readings (sensor_id, taken_at, value) VALUES
(1, '2025-03-01 10:00:00', 25.4),
(1, '2025-03-01 10:01:00', 18.2),
(1, '2025-03-01 10:02:00', 9.8),
(1, '2025-03-01 10:03:00', 31.0),
(3, '2025-03-01 10:00:00', 40.1),
(3, '2025-03-01 10:01:00', 38.7),
(3, '2025-03-01 10:02:00', 12.5),
(4, '2025-03-01 10:00:00', 0.98),
(4, '2025-03-01 10:01:00', 1.02),
(4, '2025-03-01 10:02:00', 1.50);
Sensors 1 and 3 are the HC-SR04 distance sensors on Smars and Pico Walker, and sensor 4 is Pico Walker’s MPU6050.
An aggregate squashes many rows into one answer:
SELECT COUNT(*) AS readings_taken,
MIN(value) AS lowest,
MAX(value) AS highest,
ROUND(AVG(value), 2) AS average,
ROUND(SUM(value), 1) AS total
FROM readings;
readings_taken lowest highest average total
-------------- ------ ------- ------- -----
10 0.98 40.1 17.92 179.2
COUNT(*) counts rows. COUNT(column) counts only rows where that column is not NULL, which matters for our batteries:
SELECT COUNT(battery_mah) AS known, COUNT(*) AS robots,
ROUND(AVG(battery_mah), 1) AS average
FROM robots;
known robots average
----- ------ -------
3 5 1566.7
Five robots, three known batteries, and the average ignores the two NULLs. That is usually what you want, but be aware of it.
flowchart LR
A["10 readings"] --> B["Split into groups by sensor_id"]
B --> C["Sensor 1: 4 rows"]
B --> D["Sensor 3: 3 rows"]
B --> E["Sensor 4: 3 rows"]
C --> F["One summary row each"]
D --> F
E --> F
GROUP BY splits the rows into buckets and gives each bucket its own aggregate:
SELECT sensor_id, COUNT(*) AS n,
MIN(value) AS closest, MAX(value) AS furthest
FROM readings
GROUP BY sensor_id
ORDER BY sensor_id;
sensor_id n closest furthest
--------- - ------- --------
1 4 9.8 31.0
3 3 12.5 40.1
4 3 0.98 1.5
Every column in your SELECT must either be in the GROUP BY or be wrapped in an aggregate. Here is another grouping, on the sensors table:
SELECT kind, COUNT(*) AS how_many, ROUND(SUM(price), 2) AS total_cost
FROM sensors
GROUP BY kind
ORDER BY kind;
kind how_many total_cost
-------- -------- ----------
distance 3 11.0
line 3 3.6
motion 1 3.8
GROUP_CONCAT joins the values of a group into one piece of text, which is a handy way to see what is in each bucket:
SELECT kind, GROUP_CONCAT(name, ', ') AS robots
FROM robots
GROUP BY kind
ORDER BY kind;
kind robots
------ -------------------------------
arm Arm Buddy
rover Smars, Line Chaser, Mystery Bot
walker Pico Walker
Both filter, but at different moments:
WHERE filters rows before they are grouped.HAVING filters groups after they have been summarised.Count only readings above 10 cm, per sensor, using WHERE:
SELECT sensor_id, COUNT(*) AS n
FROM readings
WHERE value > 10
GROUP BY sensor_id
ORDER BY sensor_id;
Now keep only the sensors whose average is above 5, using HAVING. You can’t put an aggregate in WHERE, because the averages don’t exist yet at that point:
SELECT sensor_id, ROUND(AVG(value), 1) AS avg_value
FROM readings
GROUP BY sensor_id
HAVING AVG(value) > 5
ORDER BY sensor_id;
sensor_id avg_value
--------- ---------
1 21.1
3 30.4
The MPU6050 (sensor 4) averages about 1.17, so its group is dropped.
How many sensors does each robot carry, including robots with none? Combine a LEFT JOIN with COUNT on the sensor column:
SELECT r.name, COUNT(s.sensor_id) AS sensors_fitted
FROM robots AS r
LEFT JOIN sensors AS s ON s.robot_id = r.robot_id
GROUP BY r.robot_id
ORDER BY r.robot_id;
name sensors_fitted
----------- --------------
Smars 2
Pico Walker 2
Arm Buddy 0
Line Chaser 2
Mystery Bot 0
Had we written COUNT(*), Arm Buddy and Mystery Bot would each show 1, because the left join still produces one row for them, with empty sensor columns. Counting a column from the right-hand table counts only real matches.
WHERE.HAVING.readings, sensors and robots.misuse of aggregate function AVG() in a WHERE.HAVING clause.Why: WHERE runs before the groups are made, so no averages exist yet.
LEFT JOIN count shows 1 for robots that have nothing.COUNT(s.sensor_id), not COUNT(*).Why: COUNT(*) counts rows, and the join keeps one row per unmatched robot.
GROUP BY, or wrap it in an aggregate.Quick quiz 1 of 3
What is the difference between WHERE and HAVING?
WHERE for row conditions and HAVING for conditions on COUNT, AVG and the like.Quick quiz 2 of 3
Five robots, three with a battery figure. What does COUNT(battery_mah) return?
COUNT(column) skips NULLs. COUNT(*) would give 5.Quick quiz 3 of 3
Why use COUNT(s.sensor_id) rather than COUNT(*) after a LEFT JOIN?
We can now summarise data. Next, let’s look at how to design the tables in the first place.
Comments