5 MIN · 2026-08-04

GROUP BY in plain words

What really happens when SQL groups rows: counting, summing and the difference between WHERE and HAVING.

GROUP BY in plain words

SELECT returns rows. But the questions that actually get asked are usually about piles: how many songs per genre, how much each country spent, which album has the longest running time. GROUP BY turns rows into piles, and the aggregate functions — COUNT, SUM, AVG — summarize each pile.

This article explains what grouping really does, using the record store behind every SQLazo exercise.

What grouping does

The catalog holds 49 tracks spread over 8 genres. Ask for them and you get 49 rows. Group them instead:

SELECT genre_id, COUNT(*) AS tracks
FROM tracks
GROUP BY genre_id;

Eight rows come back — one per genre:

genre_idtracks
111
26
39
48
56
63
73
83

Rock (1) is the big pile with 11 songs; Soul (3) has 9; Folk (4) has 8; three genres barely fill a playlist. (The names live in a separate genres table — pulling them in takes a JOIN, which is a later chapter; the ids do the job for now.)

What did the database do? First it read all 49 rows. Then it split them into piles by genre_id — 8 piles. Then COUNT(*) counted the rows inside each pile. Grouping is literally sorting the rows into boxes and applying the aggregate to each box.

SUM and AVG inside the piles

COUNT isn't the only thing that works per pile — every aggregate does: SUM, AVG, MIN, MAX. Here's the store's revenue per country:

SELECT billing_country,
       COUNT(*) AS invoices,
       ROUND(SUM(total), 2) AS revenue
FROM invoices
GROUP BY billing_country
ORDER BY revenue DESC;

The top of the table: Spain, 4 invoices, 11.40. Brazil, 3 invoices, 8.91. USA, 3 invoices, 8.13. Japan, 2 invoices, 6.54. Twelve countries in total, and if you add every pile you get 53.61 — the store's whole takings.

Each country became a pile: COUNT(*) counted the invoices inside it, SUM(total) added their totals. One GROUP BY, three summaries.

WHERE vs HAVING: filters at different moments

Two filters exist, and they work at different times. Watch:

SELECT genre_id, COUNT(*) AS tracks
FROM tracks
WHERE duration_sec > 200
GROUP BY genre_id
HAVING COUNT(*) >= 5;
genre_idtracks
19
26
37
46
56
  • WHERE runs before grouping. It drops rows: songs under 200 seconds never even reach a pile. Rock lost its two short tracks (Paper Sun, 198 s, and Copper Wire, 176 s) and went from 11 to 9.
  • HAVING runs after grouping. It drops piles: any genre that ended up with fewer than five songs vanishes (Classical with 3, Hip-Hop with 2, Bossa Nova with 3).

That's the whole rule: WHERE filters rows, HAVING filters groups. WHERE can't see counts because the counts don't exist yet when it runs — that's why HAVING exists at all.

Takeaway

GROUP BY splits rows into piles; COUNT, SUM and AVG summarize each pile; WHERE trims the rows before the piles are built and HAVING trims the piles after.

Keep exploring: the aggregates chapter covers COUNT, SUM, AVG, MIN and MAX with seven graded exercises on this same data.

Keep learning