TetSharing
SQL tutorial · 14 chapters
  1. Get started
  2. SELECT columns
  3. WHERE filters
  4. AND, OR, NOT, IN, BETWEEN
  5. ORDER BY and LIMIT
  6. LIKE patterns
  7. NULL values
  8. COUNT, SUM, AVG, MIN, MAX
  9. GROUP BY and HAVING
  10. INNER JOIN
  11. LEFT JOIN
  12. Subqueries
  13. INSERT, UPDATE, DELETE
  14. CREATE TABLE and constraints

Final SQL quiz
Cheat sheet: sql cheat sheet

Chapter 9 of 14 · By Ethan Cole · Updated November 21, 2026

SQL GROUP BY and HAVING: Totals Per Group

The previous chapter turned the whole table into one summary row. GROUP BY produces one summary row per group instead: titles per genre, copies per author, revenue per month.

How grouping works

GROUP BY genre sorts the rows into buckets, one per distinct genre. Each aggregate in the SELECT list is then computed separately for each bucket. The plain columns you select should be the grouping columns; they are the same for every row in the bucket, so each group has one well-defined value for them.

In the first query of the example, the ten books form three groups. Fiction has four titles, and its most expensive one costs 17.25. ORDER BY titles DESC, genre sorts the summary rows, using the alias defined in SELECT.

HAVING filters groups

WHERE filters individual rows before grouping. HAVING filters whole groups after the aggregates are computed, so it can use them. The second query groups books by author, adds up their stock, and keeps only authors with more than 20 copies on the shelves.

SQLite · editable

Expected output

genre | titles | top_price
Fiction | 4 | 17.25
Nonfiction | 3 | 24.5
Tech | 3 | 39.5
author_id | copies
5 | 34
4 | 23

The full order of evaluation

SQL clauses are written in one order and evaluated in another:

  1. FROM (and joins) collects rows.
  2. WHERE filters rows.
  3. GROUP BY forms groups.
  4. HAVING filters groups.
  5. SELECT computes the output columns.
  6. ORDER BY sorts, and LIMIT trims.

When a query surprises you, walk through it in that order.

Grouping by several columns

GROUP BY genre, year makes one group per distinct combination of genre and year. You can also group by an expression, for example GROUP BY substr(ordered_on, 1, 7) to group ISO dates by month.

Showing names instead of ids

The second query shows author_id, which is not very friendly. To show author names, join the authors table, which is the topic of the next chapter. Joins and GROUP BY are often used together.

Common mistakes

  • Using WHERE to filter on an aggregate; that is HAVING's job.
  • Selecting a column that is neither grouped nor aggregated. SQLite allows it but picks a value from an arbitrary row; other databases raise an error.
  • Forgetting ORDER BY; groups are not guaranteed to come out sorted.

Summary

  • GROUP BY col returns one row per distinct value, with aggregates per group.
  • WHERE filters rows before grouping; HAVING filters groups after.
  • Evaluation order: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.

Quiz: GROUP BY and HAVING

1. Which clause filters groups by an aggregate value?

HAVING runs after aggregation and can use aggregate results.

2. SELECT genre, count(*) FROM books GROUP BY genre; returns how many rows for our 3 genres?

One row per group.

3. Which runs first: WHERE or GROUP BY?

Rows are filtered before they are grouped.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.