thinQmaster Virtual testing partner Adaptive practice exams that find your weak spots and drill them. Every answer explained. From $69
Enroll

thinQmaster

Find your exam

Five questions free, before you decide.

Step 1 · choose a vendor

In one of our camps? Your practice exams come with the tuition — they are on your study page already.

Commonly confused · Data & Analytics

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

WHERE filters individual rows before they are grouped and aggregated. HAVING filters groups after GROUP BY and aggregation, so it can use aggregate functions like SUM or COUNT in its conditions.

WHERE Clause

Full definition
vs

HAVING Clause

Full definition
WHERE ClauseHAVING Clause
FiltersIndividual rowsGroups
EvaluatedBefore GROUP BYAfter GROUP BY
Aggregate functions allowedNoYes
Requires GROUP BYNoTypically used with it
Clause orderFROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY (logical order)Same order; HAVING follows GROUP BY
ExampleWHERE region = 'East'HAVING SUM(sales) > 10000
Remember it

WHERE filters rows going in; HAVING filters groups coming out.

Exam tip

A common trick shows WHERE SUM(sales) > 1000, which is invalid; the condition belongs in HAVING. Know the logical processing order of SQL clauses.

Tested on

More Data & Analytics look-alikes