BeginnerCoder

Lesson 14 — HAVING

HAVING: filter groups

WHERE filters rows before grouping. HAVING filters groups after grouping, so it can use aggregates. "Customers with more than 2 orders" needs HAVING, because the count only exists after GROUP BY.

SELECT customer, COUNT(*) FROM orders
GROUP BY customer
HAVING COUNT(*) > 2

Why it matters: "which products sold over 100 units?", "which pages have more than 10 errors?" — every threshold on a group is a HAVING.

Want it another way?
Task

Add HAVING so only customers with at least 2 orders are shown. Expected: Ann 3, Cy 2.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.