INTERACTIVE LECTURES
Summarize and compare
Guided class activity · apply the ideas from the full SQL lessons.
Open the detailed lessons for this activity
Detailed lessons in this module
Open the full explanations, examples, and SQL exercises directly.
Opening your saved work…
Sign in to save across devices → · Download your guest work before switching accounts.
Filter rows before groups, groups after aggregation
Step 4 of 4 · Database: Northshore Coffee
READ & UNDERSTAND
WHERE removes input rows before aggregation. HAVING removes groups after aggregation. An average over all menu items is different from an average over only expensive items.
A good check reconciles the grouped counts with a total computed on the same filtered population. Matching a number is evidence only when both queries measure the same thing.
Worked example
SELECT category, COUNT(*) AS items FROM menu_items WHERE price >= 5 GROUP BY category HAVING COUNT(*) >= 2 ORDER BY category;
WHERE price >= 5 changes the input population first. GROUP BY collects those expensive items into categories, and HAVING keeps only categories with at least two remaining items.
Moving the count condition into WHERE is not equivalent. Removing WHERE is valid SQL but changes both the category counts and the business question.
Your question
Return category and COUNT(*) AS item_count for categories with at least three menu items. Include all price levels; order by category.
Explain your evidence
Why would WHERE price >= 3 answer a different question?
Try a query to unlock the worked-answer discussion.
Query results
Predict the result, then run your query.Predict the result, then run your query.
Read-only activity · One statement per run · First 200 rows displayed · Checks compare the complete bounded result, not just this preview.
Explore this query freely in the playground ↗Check the save status above. Account-saved work is available in My Progress. Explanations are for discussion, not automatically graded. No prerequisite locks.