Why this lesson matters
Understand having and reason about it with a concrete relational example. Aggregate functions reduce many rows to measures. GROUP BY defines each result group, while HAVING filters those groups after aggregation.
How to reason about it
- For HAVING, the outcome to verify is: Run the statement against sample data and inspect the result before moving to the next case.
- In HAVING, keep this failure controlled: Selecting non-grouped columns beside aggregates or using WHERE when a condition depends on an aggregate leads to invalid or misleading queries.
- HAVING practice target: Calculate order count and revenue by customer, then keep only customers whose order count crosses a chosen threshold.
Practical walkthrough
In the HAVING walkthrough: Run the statement against sample data and inspect the result before moving to the next case.
example.sqlsql
SELECT customer_id, COUNT(*) AS orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) >= 5;Practice it yourself
HAVING exercise
Calculate order count and revenue by customer, then keep only customers whose order count crosses a chosen threshold.
- Record the expected result before execution
- Test one valid path and one lesson-specific failure path
- Explain in two lines which boundary owns the decision
