CodeBlanks Logo Icon CodeBlanks

Summary - HAVING

Find the top-selling category with GROUP BY and HAVING.

Summary - HAVING

HAVING filters groups. WHERE sets row-level, HAVING sets group-level conditions.

SELECT category, COUNT(*)
FROM products
GROUP BY category
HAVING COUNT(*) > 5;

Summary HAVING query.

category, COUNT(*) is selected from products. Rows are grouped by category; aggregate functions are computed per group. HAVING COUNT(*) > 5 runs after grouping and drops groups that fail the condition.

Practice Summary - HAVING

Summary: find categories with more than 5 products using HAVING.

Starter code

query.sql
-- summary having
SELECT category, COUNT(*)
FROM products
GROUP BY category

Sample solution

query.sql
SELECT category, COUNT(*)
FROM products
GROUP BY category
HAVING COUNT(*) > 5;

Expected output

category  count
Elektronik 8

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Where does a group condition go?

  • WHERE
  • HAVING Correct answer
  • FROM
  • SELECT

Explanation: A group condition goes in HAVING.

2 Summary - HAVING: match the terms

Term Meaning
HAVING group filter
WHERE row filter
AND both
OR at least one

All lessons in Intermediate