Counting, summing and grouping
Summarize many rows into one with aggregates, then summarize by group with GROUP BY and HAVING.
20 min
An aggregate function reads many rows and returns one value:
| Function | Result |
|---|---|
COUNT(*) | number of rows |
SUM(col) | total |
AVG(col) | average |
MIN(col), MAX(col) | smallest, largest |
SELECT COUNT(*), AVG(price), MAX(year) FROM books;returns one row: 10 books, the average price, the newest year. COUNT(col) counts only rows where col is not NULL; COUNT(*) counts all rows.
To get totals per genre, group the rows first:
SELECT genre, COUNT(*) AS books, ROUND(AVG(price), 2) AS avg_price
FROM books
GROUP BY genre;The database puts the rows into piles by genre, then runs each aggregate once per pile. Every column in SELECT must either be in GROUP BY or be inside an aggregate, because each output row stands for a whole pile.
WHERE filters rows before grouping. HAVING filters groups after the aggregates are calculated:
SELECT genre, COUNT(*) AS books
FROM books
WHERE stock > 0
GROUP BY genre
HAVING COUNT(*) >= 2;Read it as: take the books in stock, group by genre, keep only genres with two or more of them.
Find, for the books table, how many titles each author has, and keep only authors with more than one title.
- 1
SELECT author, COUNT(*) AS titles FROM books
We want one row per author, with a count next to it.
Show the number of books and the total stock across all books, as two columns named books and units.
Tables you can query
- books (id, title, author, genre, price, year, stock)
SELECT
FROM books;For each genre, show the genre and the average price rounded to 2 decimals, named avg_price. Show only genres that have at least 3 books.
Tables you can query
- books (id, title, author, genre, price, year, stock)
SELECT genre
FROM booksFor each status in orders, show the status and how many orders have it, as status and n. Sort by n from largest to smallest, then status A to Z.
Tables you can query
- orders (id, customer_id, order_date, status)
- order_items (order_id, book_id, qty)
SELECT status
FROM orders“I can use `WHERE COUNT(*) > 1` to keep big groups.”
WHERE is the filter you know, and the condition reads naturally.
What is actually true
WHERE runs before grouping, when no counts exist yet. Conditions on aggregates go in HAVING; conditions on ordinary columns go in WHERE.
How to catch yourself
If the condition mentions COUNT, SUM, AVG, MIN or MAX, it belongs in HAVING.
- Aggregates (
COUNT,SUM,AVG,MIN,MAX) turn many rows into one value. GROUP BYmakes one row per group; other selected columns must be grouped or aggregated.WHEREfilters rows before grouping;HAVINGfilters groups after.COUNT(*)counts rows;COUNT(col)skips NULLs.
Aggregate checks.
Which returns the highest price?
- A`SELECT MAX(price) FROM books;`
- B`SELECT TOP price FROM books;`
- C`SELECT price MAX FROM books;`
- D`SELECT HIGH(price) FROM books;`
Where does a condition on COUNT(*) go?
- AWHERE
- BHAVING
- CFROM
- DORDER BY only
With the 10 books and SELECT genre, COUNT(*) FROM books GROUP BY genre, how many rows come back?
Why is SELECT genre, title, COUNT(*) FROM books GROUP BY genre wrong in strict SQL?
- A`title` is neither grouped nor aggregated
- BCOUNT cannot follow a column
- CGROUP BY needs ORDER BY
- Dgenre is text
`SELECT (price) FROM books;` returns the mean price.
Capitalisation, spacing and punctuation are ignored when grading.
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of SQL for Beginners is planned around your week. Free.