SQL LESSON 12 OF 32

HAVING: filter your groups

WHERE filters rows before grouping. HAVING filters groups after aggregation. For example, keep only cities with more than one customer. Think: filter rows, group, then filter summaries.

हिन्दी में समझें

WHERE समूह बनने से पहले पंक्तियाँ छाँटता है। HAVING सार बनने के बाद समूह छाँटता है। पहले पंक्तियाँ, फिर समूह, फिर सार पर शर्त।

अभ्यास: कम से कम 2 उत्पाद वाली category और COUNT(*) AS total, category क्रम।

Hinglish mein samjhein

WHERE grouping se pehle rows filter karta hai. HAVING aggregation ke baad groups filter karta hai. Pehle rows, phir group, phir summary condition.

Practice: 2 ya zyada products wali category aur COUNT(*) AS total, category sorted.

A small SQL example

SELECT city, COUNT(*) AS total FROM customers GROUP BY city HAVING COUNT(*) > 1;

Examples use the fictional shop database bundled with the SQL playground: customers, products, orders, order_items and employees. The engine is SQLite; other SQL dialects may differ.

Try this example in the SQL playground →

Practise this idea

Show category and count as total for categories with at least two products, ordered by category.

Check answer & explanation
SELECT category, COUNT(*) AS total FROM products GROUP BY category HAVING COUNT(*) >= 2 ORDER BY category;

HAVING applies the count condition after grouping. A count of at least two retains categories with two or more products.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

Which filters aggregated groups?

  1. HAVING
  2. WHERE
  3. LIMIT only
Check the quiz answer

HAVING

WHERE filters rows before grouping. HAVING filters groups after aggregation. For example, keep only cities with more than one customer. Think: filter rows, group, then filter summaries.

References & corrections

Use the official SQLite documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.