Sub topic 8
Filtering rows with WHERE and groups with HAVING
Assume you know basic SELECT, FROM, WHERE, GROUP BY, and ORDER BY syntax. You should also understand that NULL represents a missing value. This chapter uses an orders table with customer_id, amount, and status columns.
Filter rows before grouping with WHERE
WHERE filters individual input rows. The database removes rows that do not match before it forms groups or calculates aggregates.
SELECT customer_id,
SUM(amount) AS paid_total
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
ORDER BY paid_total DESC;This query first keeps only paid orders. It then groups those rows by customer_id and calculates a total for each customer. Unpaid and refunded orders cannot affect the totals because they were removed first.
Use WHERE when your condition concerns row values:
status = 'paid'amount >= 100created_at >= DATE '2026-01-01'customer_id IS NOT NULL
A condition using NULL requires IS NULL or IS NOT NULL. The expression status = NULL does not match missing values.
Filter groups with HAVING
HAVING filters groups after GROUP BY has created them and after aggregate values have been calculated.
SELECT customer_id,
COUNT(*) AS order_count,
SUM(amount) AS total_amount
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
HAVING SUM(amount) > 1000
ORDER BY total_amount DESC;The WHERE clause removes unpaid orders. The GROUP BY clause creates one group per customer. The HAVING clause keeps only customers whose paid orders total more than 1000.
Because HAVING works with groups, it commonly contains aggregate functions such as SUM, COUNT, or AVG.
| Clause | Filters | Typical condition |
|---|---|---|
| `WHERE` | Individual rows | `amount >= 100` |
| `HAVING` | Groups | `SUM(amount) > 1000` |
Logical evaluation order
SQL text is written with SELECT near the beginning, but the logical processing order is different:
This order explains why an aggregate alias is often unavailable in WHERE: the aggregate does not exist at that stage. It also explains why HAVING can test SUM(amount) after grouping.
Some database systems allow this form:
SELECT customer_id,
SUM(amount) AS total_amount
FROM orders
GROUP BY customer_id
HAVING total_amount > 1000;However, alias support in HAVING varies by database. For portable SQL, repeat the aggregate expression:
HAVING SUM(amount) > 1000Avoid filtering too late
Suppose you need customers with at least two paid orders. Put the status test in WHERE, then test the group count in HAVING.
SELECT customer_id,
COUNT(*) AS paid_order_count
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
HAVING COUNT(*) >= 2;Moving status = 'paid' into HAVING changes the meaning. The group would first include every order, so the count could include unpaid orders. A condition that identifies eligible rows belongs in WHERE; a condition about the completed group belongs in HAVING.
> callout note > Apply row filters as early as possible with WHERE. This usually makes the query easier to reason about and gives the database fewer rows to group.
Exercise
Write a query that returns each customer's average paid order amount, but only for customers whose average is at least 250.
Show answer
SELECT customerid, AVG(amount) AS averageamount FROM orders WHERE status = 'paid' GROUP BY customer_id HAVING AVG(amount) >= 250;