WHERE vs HAVING mental model in SQL - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how filtering data at different stages affects the work a database does.
Specifically, how using WHERE and HAVING changes the amount of data processed.
Analyze the time complexity of these two queries.
-- Query 1: Filtering rows before grouping
SELECT department, COUNT(*)
FROM employees
WHERE salary > 50000
GROUP BY department;
-- Query 2: Filtering groups after grouping
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
The first query filters rows before grouping; the second filters groups after grouping.
Look at what repeats as data grows.
- Primary operation: Scanning all employee rows.
- How many times: Once for each row in the table.
- Grouping operation processes all filtered rows or all rows depending on WHERE or HAVING.
Imagine the table grows from 10 to 1000 employees.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Scan 10 rows, group filtered rows |
| 100 | Scan 100 rows, group filtered rows |
| 1000 | Scan 1000 rows, group filtered rows |
Filtering early with WHERE reduces rows before grouping, so less grouping work.
Filtering late with HAVING means grouping all rows first, then filtering groups.
Time Complexity: O(n)
This means the work grows linearly with the number of rows, but filtering early can reduce the constant work inside.
[X] Wrong: "WHERE and HAVING filters do the same amount of work."
[OK] Correct: WHERE filters rows before grouping, reducing data early. HAVING filters after grouping, so grouping happens on all rows first.
Understanding when filtering happens helps you explain query efficiency clearly and shows you know how databases handle data step-by-step.
What if we added a WHERE condition and a HAVING condition together? How would that affect the time complexity?
Practice
Solution
Step 1: Understand filtering before grouping
The WHERE clause filters individual rows before any grouping happens in the query.Step 2: Compare WHERE and HAVING roles
HAVING filters groups after grouping, so it cannot filter rows before grouping.Final Answer:
WHERE -> Option BQuick Check:
Filter rows before grouping = WHERE [OK]
- Using HAVING to filter rows before grouping
- Confusing GROUP BY as a filter
- Using ORDER BY to filter data
Solution
Step 1: Identify correct HAVING usage
HAVING is used to filter groups based on aggregate functions like SUM.Step 2: Check query syntax and order
SELECT store, SUM(sales) FROM sales_data GROUP BY store HAVING SUM(sales) > 1000; correctly uses GROUP BY first, then HAVING with SUM(sales) > 1000.Final Answer:
SELECT store, SUM(sales) FROM sales_data GROUP BY store HAVING SUM(sales) > 1000; -> Option CQuick Check:
Filter groups by aggregate = HAVING [OK]
- Using WHERE with aggregate functions
- Placing HAVING before GROUP BY
- Filtering rows instead of groups
orders(order_id, customer_id, amount), what will this query return?SELECT customer_id, COUNT(*) AS order_count FROM orders WHERE amount > 50 GROUP BY customer_id HAVING order_count > 2;
Solution
Step 1: Understand alias usage in HAVING
Many SQL databases allow using column aliases like order_count directly in HAVING clause, but standard SQL does not. However, most practical systems support it.Step 2: Identify correct HAVING syntax
Using alias in HAVING is often allowed; thus, the query returns customers with more than 2 orders where each order amount is over 50.Final Answer:
Customers with more than 2 orders where each order amount is over 50 -> Option DQuick Check:
HAVING filters groups; alias usage depends on SQL dialect [OK]
- Using alias in HAVING clause (may be allowed in some SQL dialects)
- Confusing WHERE and HAVING filters
- Assuming HAVING filters rows
SELECT department, AVG(salary) FROM employees HAVING AVG(salary) > 50000 WHERE department LIKE 'Sales%' GROUP BY department;
Solution
Step 1: Check SQL clause order
The correct order is WHERE, then GROUP BY, then HAVING.Step 2: Identify misplaced WHERE clause
In the query, WHERE appears after HAVING, which is invalid syntax.Final Answer:
WHERE clause used after HAVING -> Option AQuick Check:
WHERE before GROUP BY, HAVING after [OK]
- Placing WHERE after HAVING
- Forgetting GROUP BY clause
- Using HAVING without GROUP BY
Solution
Step 1: Filter rows with WHERE
Use WHERE to keep only orders with amount > 100 before grouping.Step 2: Filter groups with HAVING
Use HAVING to keep customers with more than 3 such orders (COUNT(*) > 3).Final Answer:
SELECT customer_id FROM orders WHERE amount > 100 GROUP BY customer_id HAVING COUNT(*) > 3; -> Option AQuick Check:
WHERE filters rows, HAVING filters groups [OK]
- Using HAVING to filter rows
- Placing WHERE after HAVING
- Using aggregate in WHERE clause
