Ever wondered why filtering data before or after grouping changes your results so much?
WHERE vs HAVING mental model in SQL - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a big list of sales data in a spreadsheet. You want to find which products sold more than 100 units in total. You try filtering the rows first, then adding up sales, but it's confusing and you keep making mistakes.
Manually filtering rows before or after adding totals is tricky. If you filter too early, you miss important data. If you filter too late, you waste time checking every row. It's easy to get wrong results or spend hours fixing errors.
Using WHERE and HAVING in SQL helps you filter data at the right time. WHERE filters rows before grouping, so you only work with relevant data. HAVING filters after grouping, so you can pick groups that meet conditions like total sales above 100. This makes your queries clear and correct.
SELECT product, SUM(sales) FROM sales_data WHERE sales > 100 GROUP BY product;SELECT product, SUM(sales) FROM sales_data GROUP BY product HAVING SUM(sales) > 100;This mental model lets you write precise queries that find exactly the groups or rows you want, saving time and avoiding mistakes.
A store manager wants to know which products sold more than 100 units last month. Using WHERE and HAVING correctly, they quickly get the list without missing any product or counting wrong totals.
WHERE filters rows before grouping.
HAVING filters groups after aggregation.
Using both correctly makes data queries accurate and efficient.
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
