the question: "which customers spent more than 60?"
table:
```
order_id | customer | status | amount
1 | Asha | completed | 40
2 | Asha | completed | 30
3 | Asha | refunded | 50
4 | Ben | completed | 20
5 | Ben | refunded | 90
6 | Chen | completed | 80
```
**query 1, HAVING only**
```
SELECT customer, SUM(amount) AS spent
FROM orders
GROUP BY customer
HAVING SUM(amount) > 60;
```
Asha 120, Ben 110, Chen 80. looks fine but refunds are counted as spending. Ben only really spent 20.
**query 2, WHERE on amount**
```
SELECT customer, SUM(amount) AS spent
FROM orders
WHERE amount > 60
GROUP BY customer;
```
Ben 90, Chen 80. this answers a different question: "who had a single order over 60". Asha is gone even though she spent 70 in total, and Ben is back because of his refund.
**query 3, WHERE + HAVING**
```
SELECT customer, SUM(amount) AS spent
FROM orders
WHERE status = 'completed'
GROUP BY customer
HAVING SUM(amount) > 60;
```
Asha 70, Chen 80. this is the right one.
the way i think about it:
- WHERE filters **rows**, before grouping. "which orders count?"
- HAVING filters **groups**, after grouping. "which customers make the cut?"
and you cant do `WHERE SUM(amount) > 60`, the database hasnt added anything up yet at that point so it errors.
anyone else have a go-to way of explaining this one?