Learn with Abdou

INTERACTIVE LECTURES

Summarize and compare

Guided class activity · apply the ideas from the full SQL lessons.

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.

Loading…
Table & column names only · ↑ ↓ choose · Tab insert · Esc dismiss

CURRENT DATABASE

Quick schema · Northshore Coffee

Press Esc or click outside to return to your query. PK = primary key · FK = declared foreign key.

This database has no tables yet. Create a table, then reopen the schema to see its columns.

Control Space also opens names. Enter adds a new line. Control or Command Enter runs your query.

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.