Stockertim Dictionary · Technology
HAVING clause
Also called HAVING, SQL HAVING, group filter
In short
The part of an SQL query that filters groups after they have been summarised, keeping only those whose aggregate values meet a condition.
- Intermediate
- Concept
- Checked
In detail
HAVING does for groups what WHERE does for rows. Once GROUP BY has collected rows and the aggregates have been calculated, HAVING tests each group and drops those that fail. SELECT supplier, SUM(cost) FROM purchases GROUP BY supplier HAVING SUM(cost) > 50000 lists only suppliers paid more than 50,000 in total.
The difference in timing is the whole point. WHERE runs first and sees single rows, so it cannot ask about a total that does not exist yet; writing WHERE SUM(cost) > 50000 is an error. HAVING runs after grouping, so its conditions usually involve an aggregate such as COUNT, SUM or AVG.
Both clauses often appear in the same query. WHERE year = 2026 first narrows the rows to one year; GROUP BY supplier then totals what is left; HAVING COUNT(*) >= 10 keeps suppliers with at least ten purchases in that year. Moving the year test into HAVING would give a different and usually wrong answer, because the counts would include other years.
A condition that does not involve an aggregate, such as a region name, belongs in WHERE even though HAVING may accept it, because filtering rows early is clearer and lets the database do less work. Some databases allow the alias from SELECT inside HAVING and others do not, so repeating the aggregate expression is the portable form.
Worked example
A taxi firm in Kampala has a trips table and wants drivers who completed at least 100 trips in October with average fares above USh 15,000.
- Write:
SELECT driver_id, COUNT(*) AS trips, AVG(fare) AS avg_fare FROM trips WHERE trip_month = '2026-10' GROUP BY driver_id HAVING COUNT(*) >= 100 AND AVG(fare) > 15000; - Of 60 drivers active in October, 18 meet both conditions: 18 / 60 = 30%.
The list of 18 drivers excludes anyone with too few trips for a fair average.
In practice
HAVING is used to find heavy users, duplicate records (GROUP BY email HAVING COUNT(*) > 1), suppliers above a spending limit and products with too few sales to judge. In pandas the same step is a filter applied after groupby; in a pivot table it is a value filter.
Watch out
- Putting a row condition in HAVING instead of WHERE can change which rows are counted.
- HAVING without GROUP BY treats the whole table as one group, which is rarely what was meant.
Learn it properly
- Summarising with SQL: GROUP BY, COUNT, SUM and AVG15 min lesson Free course: Data skills: SQL and Python basics
- Building a bond ladder for steady income15 min lesson Free course: Bonds & fixed income: Intermediate
- Confirmation bias: seeing only what you want to see5 min lesson Free course: Risk & psychology: Intermediate
- What is market volatility?15 min lesson Free course: Risk & psychology: Beginner
Official sources
Facts last checked on 9 November 2026.
Education, not advice: the Stockertim Dictionary explains what terms mean. It never recommends buying or selling anything.