WHERE vs HAVING in SQL — What's the Difference?

WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY and aggregation. Learn when SQL requires each one.

Published September 19, 2026

WHERE filters individual rows before any grouping or aggregation happens. HAVING filters groups after GROUP BY has combined rows and aggregate functions (COUNT, SUM, AVG, etc.) have been computed.

Common causes

  • Aggregate values like COUNT(*) or SUM(price) don't exist yet at the row level — they're only computed once GROUP BY has collapsed rows into groups — so WHERE can't reference them

How to fix it

  • Use WHERE to filter on raw column values before grouping, e.g. WHERE status = 'active'
  • Use HAVING to filter on the result of an aggregate function, e.g. HAVING COUNT(*) > 5
  • You can use both in the same query — WHERE narrows rows first, then GROUP BY aggregates, then HAVING filters the aggregated groups

Example

SELECT customer_id, COUNT(*) AS order_count
FROM orders
WHERE status = 'completed'
GROUP BY customer_id
HAVING COUNT(*) > 5;

FAQ

Can I use HAVING without GROUP BY?

Yes, though it's uncommon — without GROUP BY, the entire result set is treated as one group, so HAVING filters based on an aggregate over all rows.

More SQL articles