SQL HAVING
Filters groups after GROUP BY using an aggregate condition, e.g. HAVING COUNT(*) > 2. WHERE filters rows before grouping. Order: WHERE, GROUP BY, HAVING.
Understanding SQL HAVING in QCE Digital Solutions Unit 4
What it does: WHERE yearLevel = 7 removes rows first, then GROUP BY groups the remaining rows and counts them. HAVING must come after GROUP BY. Putting it before GROUP BY is a syntax error. AVG(mark) is an aggregate calculated for each group, so it goes in HAVING. A plain row condition like studentId = 1 goes in WHERE. HAVING filters groups, so it normally follows GROUP BY. It does not replace GROUP BY.
Common mistakes: WHERE filters rows before grouping, so it cannot use an aggregate like COUNT(*). Filtering groups by an aggregate needs HAVING COUNT(*) > 2 after GROUP BY. The table is called students, not student. The FROM clause must name a real table or the query fails.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 334–401) and word search (Modules 127–401).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
HAVINGFill in the Blanks
Spot the Error
Student with a class code? Join your class →