Module 3 · Sorting and summarising

Filter groups with HAVING

By the end you will be able to keep only the groups that meet a condition.

About 12 min

0 of 52 lessons

Before you start, you need

Create per-group summaries with GROUP BY

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.

Minimal syntax
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?

SQL
SELECT category, COUNT(*) AS products
FROM products
GROUP BY category
HAVING COUNT(*) >= 2;
Example result: 3 rows
categoryproducts
Accessories3
Audio2
Displays2
  1. Products are grouped by category. Then the rows in each group are counted.
  2. HAVING COUNT(*) >= 2 keeps the groups with two or more products. The limit of 2 is included.
  3. WHERE COUNT(*) >= 2 does not work: the group count does not exist yet when rows are being filtered.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Filter groups by a sum

Not started

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.

Key takeaways

  • WHERE filters rows before grouping.
  • HAVING filters the resulting groups.
  • Choose the filter according to what the condition measures.

Next: Practice: combine filters and summaries