Why this lesson matters
Understand group by 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 GROUP BY, the outcome to verify is: Run the statement against sample data and inspect the result before moving to the next case.
- In GROUP BY, 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.
- GROUP BY practice target: Calculate order count and revenue by customer, then keep only customers whose order count crosses a chosen threshold.
Practical walkthrough
In the GROUP BY walkthrough: Run the statement against sample data and inspect the result before moving to the next case.
example.sqlsql
SELECT status, COUNT(*)
FROM orders
GROUP BY status;Practice it yourself
GROUP BY 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
