By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
You’re analyzing sales data for an e-commerce company. Your boss asks: "Show me all product categories where the average order value is over $100, but only for orders placed in the last 30 days."
You write a query, but it either: - Returns all categories (ignoring the $100 threshold), or - Filters out all data (because you applied the condition too early).
Why does this happen?Because WHERE and HAVING do similar but critically different things in SQL. Mix them up, and your analysis is either wrong or inefficient—costing time, money, and credibility.
WHERE
HAVING
Superpower you gain:You’ll write precise, efficient queries that filter data at the right stage—saving compute, time, and headaches.
SUM
AVG
COUNT
WHERE order_date > '2023-01-01'
AVG(sales) > 100
HAVING AVG(order_value) > 100
SUM(sales)
GROUP BY
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
AVG(sales) AS avg_sales
SELECT
HAVING category = 'Electronics'
category
sql CREATE TABLE sales ( order_id INT, product_category VARCHAR(50), order_value DECIMAL(10,2), order_date DATE );
sql INSERT INTO sales VALUES (1, 'Electronics', 150.00, '2023-05-01'), (2, 'Electronics', 200.00, '2023-05-02'), (3, 'Clothing', 50.00, '2023-05-01'), (4, 'Clothing', 30.00, '2023-05-03'), (5, 'Electronics', 80.00, '2023-04-01'); -- Old data (should be excluded)
Write a query that: 1. Only includes orders from May 2023 (WHERE).2. Groups by product_category.3. Shows only categories where the average order value > $100 (HAVING).
product_category
SELECT product_category, AVG(order_value) AS avg_order_value FROM sales WHERE order_date >= '2023-05-01' AND order_date < '2023-06-01' GROUP BY product_category;
Output:| product_category | avg_order_value | |------------------|-----------------| | Electronics | 175.00 | | Clothing | 40.00 |
SELECT product_category, AVG(order_value) AS avg_order_value FROM sales WHERE order_date >= '2023-05-01' AND order_date < '2023-06-01' GROUP BY product_category HAVING AVG(order_value) > 100;
Output:| product_category | avg_order_value | |------------------|-----------------| | Electronics | 175.00 |
Mistake: Using WHERE instead of HAVING for the aggregation:
-- ❌ WRONG: This will fail because WHERE can't filter on aggregated results SELECT product_category, AVG(order_value) AS avg_order_value FROM sales WHERE order_date >= '2023-05-01' AND order_date < '2023-06-01' AND AVG(order_value) > 100 -- ERROR: "aggregate functions not allowed in WHERE" GROUP BY product_category;
Fix: Replace WHERE with HAVING for the aggregation condition.
sql HAVING AVG(order_value) > 100 -- Hard to read HAVING avg_order_value > 100 -- Clearer (if alias is defined in SELECT)
EXPLAIN
ORDER BY
✅ WHERE
"You need to filter groups where COUNT(*) > 5. Which clause do you use?"
COUNT(*) > 5
✅ HAVING
"Why does this query fail?" sql SELECT department, AVG(salary) AS avg_salary FROM employees WHERE avg_salary > 50000 GROUP BY department;
sql SELECT department, AVG(salary) AS avg_salary FROM employees WHERE avg_salary > 50000 GROUP BY department;
"You’re analyzing customer orders. You need to: 1. Exclude orders under $10. 2. Show only cities with more than 5 orders. Which clauses do you use?"
Answer:
SELECT city, COUNT(*) AS order_count FROM orders WHERE order_value >= 10 -- Filter rows first GROUP BY city HAVING COUNT(*) > 5; -- Filter groups after
Write a query that: 1. Shows the total sales per product category.2. Only includes categories with total sales > $500.3. Excludes orders from before 2023.
Solution:
SELECT product_category, SUM(order_value) AS total_sales FROM sales WHERE order_date >= '2023-01-01' GROUP BY product_category HAVING SUM(order_value) > 500;
Why it works:- WHERE filters out old orders before aggregation.- GROUP BY groups by category.- HAVING filters groups after aggregation.
WHERE column = value
HAVING AVG(column) > 100
FROM → WHERE → GROUP BY → HAVING → SELECT
HAVING avg_sales > 100
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.