SQL aggregation: GROUP BY, HAVING and avoiding double counting
For support analysts, operations staff and developers who write reporting queries and need totals they can defend. It builds on SQL fundamentals for support staff: you should already write SELECT queries with WHERE and joins. Covers what a grouped row represents, WHERE versus HAVING versus FILTER, the join fan-out that silently inflates sums and counts, how NULLs and empty groups change results, and subtotals and per-row totals with ROLLUP and window functions. Examples use PostgreSQL 18 on a fictional service desk database; most of it applies to other SQL databases, but check your product's documentation. Run reporting queries on the replica or read-only account you've been given, and summarise rather than copy personal data. Ends with a supervisor-graded reporting task.
- Level
- Intermediate
- Length
- About 100 minutes
- Contents
- 5 lessons · final exam
- Status
- Published · updated 10 Oct 2026
Skills you'll practise
- Write a GROUP BY query with count, sum, avg, min or max and state what one output row represents
- Choose between WHERE, HAVING and FILTER to restrict rows before or after aggregation
- Identify double counting caused by joining several child tables before aggregating, and fix it
- Calculate how NULL values and empty groups change count, sum and avg results, using coalesce where zero is meant
- Use window functions and ROLLUP to show group totals and subtotals without losing detail rows
Course outline
- 1.Write a GROUP BY query and state what one output row representsLesson · 16 min
- 2.Choose between WHERE, HAVING and FILTERLesson · 16 min
- 3.Identify and fix double counting caused by joins before aggregationLesson · 20 min
- 4.Calculate how NULLs and empty groups change aggregate resultsLesson · 14 min
- 5.Use window functions and ROLLUP to show totals without losing detailLesson · 14 min
- 6.SQL aggregation: GROUP BY, HAVING and avoiding double counting: knowledge checkKnowledge check · 14 questions
- 7.SQL aggregation: GROUP BY, HAVING and avoiding double counting: practical exerciseKnowledge check · 1 question
- 8.Final exam10 questions · passing it completes the course, so people who already know the material can test out
Sources it draws on
The lessons and questions are written from these references, so learners can go back to the original.
- PostgreSQL 18 documentation, 2.7 Aggregate Functions (tutorial: GROUP BY, HAVING, FILTER)
- PostgreSQL 18 documentation, 7.2 Table Expressions (GROUP BY and HAVING; GROUPING SETS, CUBE and ROLLUP)
- PostgreSQL 18 documentation, 9.21 Aggregate Functions (count, sum, avg; nulls and empty input; GROUPING)
- PostgreSQL 18 documentation, 4.2 Value Expressions (aggregate expressions, DISTINCT, FILTER)
- PostgreSQL 18 documentation, 3.5 Window Functions (tutorial)
- PostgreSQL 18 documentation: SELECT (order of processing)
- PostgreSQL 18 documentation, 2.6 Joins Between Tables (outer joins)
See it with your own jobs and topics
Tell us about your team and we'll walk you through setup, from choosing jobs to your first skills check.