HAVING vs WHERE in SQL
Both filter. The difference is when:
- WHERE runs first, on individual rows, before anything is grouped. Use it for "only 2026 orders" or "only Europe".
- HAVING runs after GROUP BY, on the totals. Use it for "only customers with 3+ orders" or "only months over $10k".
Writing WHERE COUNT(*) > 3 fails because when WHERE runs, nothing has been counted yet.

Syntax
SELECT col, SUM(x)
FROM table
WHERE row_condition
GROUP BY col
HAVING SUM(x) > 100;
Try itEdit the query, then Run
| customer_id | orders | units |
|---|---|---|
| 290 | 5 | 20 |
| 324 | 3 | 13 |
| 297 | 4 | 13 |
| 322 | 4 | 12 |
| 181 | 2 | 12 |
| 162 | 3 | 12 |
| 87 | 4 | 12 |
| 365 | 3 | 11 |
| 343 | 2 | 11 |
| 213 | 2 | 11 |
Showing 10 of 73 rows. Press Run to see them all.
Read it in order: take orders from April on (WHERE), bucket them per customer (GROUP BY), keep buckets with at least 2 orders (HAVING), sort (ORDER BY). In Excel that's filtering the source data, building a pivot, then applying a value filter to the pivot.
Your turn
Which countries have more than 20 customers who came from paid_search? Show country and customers.
WHERE acquisition_channel = 'paid_search', GROUP BY country, HAVING COUNT(*) > 20.
This page on one card
