Member-only story
SQL 101: Aggregates On Aggregates On Aggregates
A Beginner's Guide to SQL Aggregate Functions
5 min readNov 7, 2022
Aggregates are one of the most powerful SQL features. They are integral to data analysis and are pretty fun to write!
In the SQL world, aggregates are functions applied to a group of rows to retrieve a single "aggregate" value. For example, let's say you have rows of high-score data that look like this, and you want to write a query that identifies the maximum high score.
To do this, you can use the max() aggregate function. Here's how that works:
select max(score) from high_scores;
-- returns 9409
You can use other aggregate functions in the same way. The most common aggregate functions in addition to max() are min(), avg(), sum(), and count().
select min(score) from high_scores;
-- returns 2441select avg(score) from high_scores;
-- returns 6276select sum(score) from high_scores;
-- returns 50211select count(score) from high_scores;
-- returns 8