Aggregates with
WHERE Filters
Learn how to combine aggregate functions like COUNT, SUM, and AVG with WHERE clauses to compute summaries on filtered subsets of your data.
What Are Aggregate Functions?
Aggregate functions take many rows of data and collapse them into a single summary value. Think of them as calculators that operate on entire columns at once.
What Does WHERE Do?
The WHERE clause is a row-level filter. Before any aggregation happens, WHERE examines each row individually and keeps only those that match the condition.
Why Combine Aggregates with WHERE?
Real questions are rarely about all the data. You almost always need summaries of a specific subset: sales from last month, students who passed, orders above a certain amount.
The Core Pattern
The WHERE clause never sees the aggregate result: it runs before the aggregate is calculated. This is the single most important concept in this lesson.
SQL Execution Order
WHERE vs HAVING
Students often confuse WHERE and HAVING. They both filter, but they operate at different stages of query processing.
| WHERE | HAVING | |
|---|---|---|
| Filters | Individual rows | Groups (after GROUP BY) |
| Runs | Before aggregation | After aggregation |
| Can reference | Column values | Aggregate results |
| Example | WHERE price \(>\) 10 | HAVING SUM(price) \(>\) 100 |
How to Build the Query
Example 1: COUNT with WHERE
Question: How many students scored above 80 on the exam?
| name | score |
|---|---|
| Alice | 92 |
| Bob | 55 |
| Clara | 88 |
| Dan | 42 |
| Eve | 95 |
| Frank | 61 |
Example 2: SUM with WHERE
Question: What is the total revenue from orders placed in January 2024?
Example 3: AVG with Multiple Conditions
Question: What is the average price of electronics that are currently in stock?
Watch Out for NULL Values
Aggregate functions handle NULLs in specific ways, and WHERE interacts with NULLs too. Understanding this prevents subtle bugs.
Adding GROUP BY to the Mix
When you use GROUP BY alongside WHERE, the execution order is: FROM → WHERE → GROUP BY → HAVING → SELECT.
The Full Query Pipeline
Common Mistakes
Quick Reference Table
| Pattern | SQL Template | When to Use |
|---|---|---|
| Simple aggregate | SELECT AGG(col) FROM t | Summary of all rows |
| Aggregate + WHERE | SELECT AGG(col) FROM t WHERE ... | Summary of filtered rows |
| Grouped aggregate | ... GROUP BY col | Summary per category |
| Grouped + WHERE | ... WHERE ... GROUP BY col | Filter rows, then group |
| Grouped + HAVING | ... GROUP BY col HAVING AGG(...) ... | Filter groups by aggregate |
Question 1 of 5
You want to find the total sales amount for orders placed in 2024. Which query is correct?
Question 2 of 5
Why does \(\text{WHERE AVG(score)} \gt 80\) cause an error?
Question 3 of 5
Given a products table, which query counts only in-stock items in the "Books" category?
Question 4 of 5
What is the correct execution order when a SQL query contains FROM, WHERE, GROUP BY, and SELECT?
Question 5 of 5
A table has 100 rows. 20 rows have a NULL value in the email column. What does SELECT COUNT(email) FROM users; return?