HAVING Clause
Filter groups after aggregation
✅ What You Will Learn
HAVING filters groups created by GROUP BY — the same way WHERE filters individual rows. You cannot use WHERE to filter on aggregate results like SUM or COUNT because WHERE runs before grouping happens. HAVING runs after.
A simple rule: use WHERE to filter rows before grouping, use HAVING to filter groups after grouping.
📋 The orders table used in examples
| order_id | product | amount | city |
|---|---|---|---|
| 1001 | Laptop | 45000 | Delhi |
| 1002 | Mobile Phone | 18000 | Noida |
| 1003 | Headphones | 3500 | Gurgaon |
| 1004 | Laptop | 52000 | Delhi |
| 1005 | Tablet | 28000 | Noida |
| 1006 | Laptop | 48000 | Delhi |
Syntax
Examples
📌 Key Points to Remember
- ✓HAVING filters groups — WHERE filters rows
- ✓HAVING goes after GROUP BY, before ORDER BY
- ✓You can use any aggregate function in HAVING (SUM, COUNT, AVG, etc.)
- ✓You can combine WHERE and HAVING in the same query
- ✓SQL execution order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY
🏢 Real-World Application
HAVING is essential for any report that filters based on aggregate totals. "Show only product categories with more than 100 orders this month" — HAVING COUNT(*) > 100. "Find salespeople whose monthly target exceeds ₹5 lakh" — HAVING SUM(sales) > 500000. "Show only cities where average order value is above ₹2,000" — HAVING AVG(order_value) > 2000. These are standard business reporting queries. The combination of GROUP BY and HAVING is the core of SQL-based KPI reporting.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What is HAVING in SQL?
HAVING filters the results of a GROUP BY query. It works like WHERE but applies after grouping and can use aggregate functions like SUM(), COUNT(), AVG(). Example: HAVING COUNT(*) > 5 keeps only groups with more than 5 rows.
Can I use HAVING without GROUP BY?
Technically yes — HAVING without GROUP BY treats the entire result as one group. But this is rarely useful. HAVING is almost always used with GROUP BY.
Can I use both WHERE and HAVING in the same query?
Yes. WHERE filters rows before grouping; HAVING filters groups after grouping. SELECT product, SUM(amount) FROM orders WHERE order_date > '2026-01-01' GROUP BY product HAVING SUM(amount) > 50000 uses both.
What aggregate functions can I use with HAVING?
All standard aggregate functions work with HAVING: COUNT(), SUM(), AVG(), MIN(), MAX(). You can also use expressions like HAVING SUM(amount) / COUNT(*) > 1000.
What is the order of SQL clauses when using GROUP BY and HAVING?
The correct order is: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT. Each clause must appear in this sequence or you will get a syntax error.
✏️ Practice Exercise
Write a query to find all products where the average order amount is above ₹15,000.
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.