Aggregate Functions
Result:
| products | units | avg_price | cheapest | dearest |
|---|---|---|---|---|
| 8 | 980 | 251.06 | 3.50 | 899.00 |
COUNT(*) counts rows. COUNT(email) counts rows where email is not NULL, and COUNT(DISTINCT city) counts different cities:
Result:
| all_rows | with_email | cities |
|---|---|---|
| 6 | 5 | 4 |
GROUP BY
Result:
| category | products | avg_price |
|---|---|---|
| Electronics | 3 | 525.67 |
| Furniture | 3 | 141.00 |
| Stationery | 2 | 4.25 |
HAVING Filters Groups
WHERE filters rows before grouping. HAVING filters groups after grouping:
Result:
| customer_id | orders |
|---|---|
| 1 | 3 |
| 2 | 2 |
| 3 | 2 |
ONLY_FULL_GROUP_BY
MySQL 5.7 and 8.0 enable the SQL mode ONLY_FULL_GROUP_BY, so a selected column must be grouped or aggregated:
ERROR 1055: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'shop.products.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Fix it by grouping the column, aggregating it, or picking one with ANY_VALUE() when you do not care which row:
Result:
| category | has_name | n |
|---|---|---|
| Electronics | 1 | 3 |
| Furniture | 1 | 3 |
| Stationery | 1 | 2 |
ONLY_FULL_GROUP_BY. Then MySQL returns an arbitrary row's value for the unlisted column, which is a silent bug.
GROUP_CONCAT: A List in One Cell
Result:
| category | product_names |
|---|---|
| Electronics | Headphones, Laptop, Phone |
| Furniture | Chair, Desk, Lamp |
| Stationery | Notebook, Pen Pack |
The result is cut at group_concat_max_len (1024 bytes by default). Raise it with SET SESSION group_concat_max_len = 100000; for long lists.
Grouping by Date Parts
Result:
| month | orders |
|---|---|
| 2025-01 | 1 |
| 2025-02 | 2 |
| 2025-03 | 2 |
| 2025-04 | 2 |
| 2025-05 | 2 |
| 2025-06 | 1 |
WITH ROLLUP: Subtotals and a Grand Total
Result:
| category | products | units |
|---|---|---|
| Electronics | 3 | 145 |
| Furniture | 3 | 35 |
| Stationery | 2 | 800 |
| NULL | 8 | 980 |
The extra last row, with category NULL, is the grand total. Use GROUPING() (MySQL 8.0.1+) to tell a rollup NULL from a real NULL:
Result:
| category | products |
|---|---|
| Electronics | 3 |
| Furniture | 3 |
| Stationery | 2 |
| ALL CATEGORIES | 8 |
GROUP BY a, b WITH ROLLUP. PostgreSQL and SQL Server use ROLLUP(a, b).
Order of Work
FROMandJOIN: build the rowsWHERE: remove rowsGROUP BY: make groupsHAVING: remove groupsSELECT: compute columns and aliasesORDER BYandLIMIT
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
Selecting a column not in GROUP BY | Error 1055 | Group it, aggregate it or use ANY_VALUE() |
Aggregates in WHERE | Error | Use HAVING |
COUNT(col) expecting all rows | NULLs skipped | Use COUNT(*) |
GROUP_CONCAT truncated silently | Missing items | Raise group_concat_max_len |
Disabling ONLY_FULL_GROUP_BY | Random values | Keep it on |
Practice
- Count orders per status.
- List customers with total spend above 500, using a join and
HAVING. - Show each city with a
GROUP_CONCATof customer names. - Add
WITH ROLLUPto the per-category report.
Conclusion
Aggregates summarize, GROUP BY makes groups, HAVING filters them, GROUP_CONCAT builds lists, and WITH ROLLUP adds subtotals. Keep ONLY_FULL_GROUP_BY on.

