Before you start, you need
Filter the groups after the calculation
You can already count products per category. Now you want to show only the categories with at least two products. That condition is about the whole group, not about a single product.
HAVING filters after grouping. HAVING COUNT(*) >= 2 keeps the groups whose count is 2 or more. It goes after GROUP BY.
WHERE and HAVING answer different questions. WHERE stock > 0 decides which products go into the calculation. HAVING COUNT(*) >= 2 decides which categories to display after counting them.
The same happens with averages: «employees who earn more than 30,000» is not the same as «departments with an average above 30,000». In Sales there are salaries of 33,000, 28,000 and 36,000; if you remove 28,000 before calculating, you change the average you are measuring.
SELECT column_name, COUNT(*) AS quantity
FROM table_name
GROUP BY column_name
HAVING COUNT(*) >= 2;See it in action
Which categories have at least two products?
SELECT category, COUNT(*) AS products
FROM products
GROUP BY category
HAVING COUNT(*) >= 2;| category | products |
|---|---|
| Accessories | 3 |
| Audio | 2 |
| Displays | 2 |
- Products are grouped by category. Then the rows in each group are counted.
- HAVING COUNT(*) >= 2 keeps the groups with two or more products. The limit of 2 is included.
- WHERE COUNT(*) >= 2 does not work: the group count does not exist yet when rows are being filtered.
- If you add WHERE stock > 0, you will count only available products. That changes the population being measured, not just the presentation.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Keep the averages above the limit
Calculate the average salary per department. Return department and the average with the alias average; keep only averages above 30000.
Expected result: 4 rows with the columns department, average, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The limit applies to the whole department's average, not to an individual salary.
Calculate AVG(salary).
Filter the summary with HAVING.
Solution explained
SELECT department, AVG(salary) AS average FROM employees GROUP BY department HAVING AVG(salary) > 30000;The condition is applied after calculating the average for each department.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Filter groups by a sum
Show category and SUM(stock) as units. Keep only the categories that add up to at least 10 units.
Expected result: 2 rows with columns category, units, in that order. The order of the rows does not matter.
View tables and data
Open this panel to see the full data.
The requirement refers to the units of the whole category, not to the stock of one product.
Group by category and calculate SUM(stock) before deciding which groups to keep.
The group threshold is HAVING SUM(stock) >= 10.
Solution explained
SELECT category, SUM(stock) AS units FROM products GROUP BY category HAVING SUM(stock) >= 10;Accessories (55) and Audio (11) pass; Storage (9) falls just short. The threshold measures the group's sum: in Audio, Speaker has 0 units and the category still passes.
Key takeaways
- WHERE filters rows before grouping.
- HAVING filters the resulting groups.
- Choose the filter according to what the condition measures.