GROUP BY &
HAVING
Learn to aggregate data into meaningful groups and filter those groups to extract powerful business insights from SQL databases.
What Is Data Aggregation?
In business, raw data contains individual records: every sale, every transaction, every customer interaction. Aggregation means combining multiple rows into a single summary value: a total, an average, a count, or a maximum.
Why GROUP BY Matters
Aggregate functions alone produce a single number for the entire table. But business questions are rarely that simple. You want totals per department, averages per region, or counts per product category.
Introducing HAVING
Once you have grouped results, you often want to filter those groups. For instance: "Show me only regions with total sales above $100." This is where HAVING comes in.
The SQL Syntax
SQL Execution Order
WHERE vs HAVING
| WHERE | HAVING | |
|---|---|---|
| Filters | Individual rows | Grouped results |
| Runs | Before GROUP BY | After GROUP BY |
| Can use aggregates? | ✕ No | ✓ Yes |
| Example | WHERE \( \text{year} = 2024 \) | HAVING \( \text{COUNT}(*) > 5 \) |
| Analogy | Pick which students take the test | Filter classes by average score |
How to Apply GROUP BY & HAVING
Example 1: Basic GROUP BY
Question: How many employees work in each department?
| department | emp_count |
|---|---|
| Marketing | 12 |
| Engineering | 34 |
| Sales | 21 |
| Finance | 8 |
Example 2: Adding WHERE Before Grouping
Question: What is the total revenue per product category, but only for orders placed in 2024?
| category | total_rev |
|---|---|
| Electronics | $482,000 |
| Clothing | $215,000 |
| Home & Garden | $98,500 |
Example 3: Filtering Groups with HAVING
Question: Which product categories had total 2024 revenue above $100,000?
| category | total_rev | Passes HAVING? |
|---|---|---|
| Electronics | $482,000 | ✓ Yes |
| Clothing | $215,000 | ✓ Yes |
| Home & Garden | $98,500 | ✕ No |
Using Multiple Aggregates
You can combine several aggregate functions in one query to get a richer picture of each group.
Grouping by Multiple Columns
You can group by more than one column to create finer-grained buckets.
| region | product_type | total_qty |
|---|---|---|
| East | Widget A | 340 |
| East | Widget B | 210 |
| West | Widget A | 180 |
| West | Widget B | 295 |
The Golden Rule of GROUP BY
GROUP BY dept
"dept" is in GROUP BY
GROUP BY dept
"name" is not grouped or aggregated
Why? Once rows are collapsed into groups, SQL doesn't know which individual "name" to display for a group of many employees. Every value must be summarized or be part of the grouping key.
Common Mistakes
Summary Cheat Sheet
| Concept | Purpose | Syntax Placement |
|---|---|---|
| GROUP BY | Split rows into groups | After WHERE |
| HAVING | Filter groups by aggregate | After GROUP BY |
| \( \text{COUNT}(*) \) | Number of rows per group | In SELECT or HAVING |
| \( \text{SUM}(\text{col}) \) | Total of a column per group | In SELECT or HAVING |
| \( \text{AVG}(\text{col}) \) | Average per group | In SELECT or HAVING |
| WHERE | Filter individual rows | Before GROUP BY |
Question 1 of 5
Which SQL clause is used to filter groups after aggregation?
Question 2 of 5
What is wrong with this query?
SELECT department, \( \text{COUNT}(*) \) FROM employees WHERE \( \text{COUNT}(*) > 3 \) GROUP BY department;
Question 3 of 5
Given this query:
SELECT city, AVG(salary) AS avg_sal FROM staff GROUP BY city HAVING \( \text{AVG}(\text{salary}) > 50000 \);
What does this return?
Question 4 of 5
In SQL's execution order, which happens first?
Question 5 of 5
You write: SELECT region, product, \( \text{SUM}(\text{sales}) \) FROM orders GROUP BY region;
Why will most databases reject this?