HAVING for
Group Filtering
Learn how to filter grouped data in SQL using the HAVING clause: the tool that WHERE cannot replace.
What is the HAVING Clause?
When you use GROUP BY in SQL, you create summary rows: one row per group. But what if you only want groups that meet a certain condition? That is exactly what HAVING does.
Think of it this way: WHERE is the bouncer at the door (checks rows before they enter). HAVING is the judge at the end (checks groups after they have been formed).
Why Can't WHERE Do This?
WHERE runs before GROUP BY, so it has no access to aggregate values like \( \text{COUNT}() \), \( \text{SUM}() \), or \( \text{AVG}() \). Trying to use an aggregate in WHERE causes an error.
SELECT department, COUNT(*)
FROM employees
WHERE COUNT(*) > 5 -- Aggregate not allowed here
GROUP BY department;
SQL Execution Order
Understanding when each clause runs is the key to mastering HAVING. SQL does not execute top-to-bottom like you write it:
The HAVING Syntax
FROM table_name
WHERE condition_on_rows
GROUP BY column
HAVING AGG_FUNC(column) condition;
The WHERE clause is optional. You can use HAVING without WHERE, but you must have GROUP BY before HAVING.
WHERE vs HAVING
| WHERE | HAVING | |
|---|---|---|
| Filters | Individual rows | Groups |
| Runs | Before GROUP BY | After GROUP BY |
| Aggregates? | ✕ Not allowed | ✓ Required |
| Requires GROUP BY? | No | Yes |
| Example | WHERE salary > 50000 | HAVING \( \text{AVG}(\text{salary}) \) > 50000 |
How to Apply HAVING
Example 1: Basic HAVING
Question: Find departments with more than 3 employees.
FROM employees
GROUP BY department
HAVING COUNT(*) > 3;
| department | emp_count |
|---|---|
| Marketing | 2 |
| Engineering | 7 |
| HR | 3 |
| Sales | 5 |
Example 2: HAVING with AVG
Question: Find product categories where the average price exceeds $50.
FROM products
GROUP BY category
HAVING AVG(price) > 50;
| category | avg_price |
|---|---|
| Electronics | $89.50 |
| Books | $22.30 |
| Furniture | $175.00 |
| Clothing | $34.80 |
Example 3: WHERE + HAVING
Question: Among active employees only, find departments with total salary over $200,000.
FROM employees
WHERE status = 'active' -- row filter
GROUP BY department
HAVING SUM(salary) > 200000; -- group filter
Visual Walkthrough
Multiple Conditions in HAVING
You can combine multiple aggregate conditions using AND and OR, just like in WHERE.
AVG(revenue) AS avg_rev
FROM retail_stores
GROUP BY city
HAVING COUNT(*) >= 3
AND AVG(revenue) > 100000;
HAVING with ORDER BY
After filtering groups, you can sort the surviving groups with ORDER BY, which runs last.
FROM books
GROUP BY genre
HAVING COUNT(*) > 10
ORDER BY book_count DESC;
Aggregate Functions in HAVING
Common Mistakes
Summary Cheat Sheet
| Clause | Purpose | Position |
|---|---|---|
| SELECT | Choose columns/aggregates | Written 1st, runs 5th |
| FROM | Pick table(s) | Written 2nd, runs 1st |
| WHERE | Filter rows | Written 3rd, runs 2nd |
| GROUP BY | Form groups | Written 4th, runs 3rd |
| HAVING | Filter groups | Written 5th, runs 4th |
| ORDER BY | Sort results | Written 6th, runs 6th |
Question 1 of 5
Which clause is used to filter groups based on aggregate values?
Question 2 of 5
What is wrong with this query?
FROM customers
WHERE COUNT(*) > 10
GROUP BY city;
Question 3 of 5
In what order does SQL execute these clauses?
Question 4 of 5
Which query finds categories where total sales exceed $1000?
Question 5 of 5
You want only active users, grouped by country, showing countries with average age over 25. Which clauses do you need?