Bird
Raised Fist0
SQLquery~20 mins

WHERE vs HAVING mental model in SQL - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
WHERE vs HAVING Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding WHERE clause filtering
Which statement best describes when the WHERE clause is applied in an SQL query?
AIt filters rows before any grouping or aggregation happens.
BIt filters groups after aggregation is done.
CIt filters columns after the SELECT statement.
DIt filters the final result set after ORDER BY.
Attempts:
2 left
💡 Hint
Think about when rows are removed in the process: before or after grouping?
🧠 Conceptual
intermediate
2:00remaining
Understanding HAVING clause filtering
Which statement best describes when the HAVING clause is applied in an SQL query?
AIt filters individual rows before grouping.
BIt filters groups after aggregation is done.
CIt filters columns before SELECT.
DIt filters rows after ORDER BY.
Attempts:
2 left
💡 Hint
Think about filtering groups, not rows.
query_result
advanced
3:00remaining
Output of WHERE vs HAVING filtering
Given the table sales with columns region and amount, what is the output of this query?

SELECT region, SUM(amount) AS total FROM sales WHERE amount > 100 GROUP BY region;
SQL
Table sales:
region | amount
-------|-------
East   | 150
East   | 90
West   | 200
West   | 50
South  | 120
South  | 80
A[{"region": "East", "total": 90}, {"region": "West", "total": 50}, {"region": "South", "total": 80}]
B[{"region": "East", "total": 240}, {"region": "West", "total": 250}, {"region": "South", "total": 200}]
C[{"region": "East", "total": 150}, {"region": "West", "total": 200}, {"region": "South", "total": 120}]
D[{"region": "East", "total": 0}, {"region": "West", "total": 0}, {"region": "South", "total": 0}]
Attempts:
2 left
💡 Hint
Remember WHERE filters rows before grouping.
query_result
advanced
3:00remaining
Output of HAVING filtering after aggregation
Given the same sales table, what is the output of this query?

SELECT region, SUM(amount) AS total FROM sales GROUP BY region HAVING SUM(amount) > 200;
SQL
Table sales:
region | amount
-------|-------
East   | 150
East   | 90
West   | 200
West   | 50
South  | 120
South  | 80
A[{"region": "East", "total": 240}, {"region": "West", "total": 250}]
B[{"region": "East", "total": 240}, {"region": "West", "total": 250}, {"region": "South", "total": 200}]
C[{"region": "South", "total": 200}]
D[]
Attempts:
2 left
💡 Hint
HAVING filters groups after SUM is calculated.
📝 Syntax
expert
2:00remaining
Identify the syntax error in combined WHERE and HAVING query
Which option contains a syntax error in this query?

SELECT region, COUNT(*) FROM sales WHERE region = 'East' HAVING COUNT(*) > 1 GROUP BY region;
AThe COUNT(*) function cannot be used in HAVING.
BThe WHERE clause cannot be used with GROUP BY.
CThe GROUP BY clause must come before HAVING, so this query is correct.
DThe HAVING clause appears before GROUP BY, causing a syntax error.
Attempts:
2 left
💡 Hint
Check the order of clauses in SQL syntax.

Practice

(1/5)
1. Which clause should you use to filter rows before grouping in a SQL query?
easy
A. HAVING
B. WHERE
C. GROUP BY
D. ORDER BY

Solution

  1. Step 1: Understand filtering before grouping

    The WHERE clause filters individual rows before any grouping happens in the query.
  2. Step 2: Compare WHERE and HAVING roles

    HAVING filters groups after grouping, so it cannot filter rows before grouping.
  3. Final Answer:

    WHERE -> Option B
  4. Quick Check:

    Filter rows before grouping = WHERE [OK]
Hint: Use WHERE for rows, HAVING for groups after grouping [OK]
Common Mistakes:
  • Using HAVING to filter rows before grouping
  • Confusing GROUP BY as a filter
  • Using ORDER BY to filter data
2. Which of the following SQL queries correctly filters groups having a total sales greater than 1000?
easy
A. SELECT store, SUM(sales) FROM sales_data WHERE SUM(sales) > 1000 GROUP BY store;
B. SELECT store, SUM(sales) FROM sales_data WHERE sales > 1000 GROUP BY store;
C. SELECT store, SUM(sales) FROM sales_data GROUP BY store HAVING SUM(sales) > 1000;
D. SELECT store, SUM(sales) FROM sales_data HAVING SUM(sales) > 1000 GROUP BY store;

Solution

  1. Step 1: Identify correct HAVING usage

    HAVING is used to filter groups based on aggregate functions like SUM.
  2. 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.
  3. Final Answer:

    SELECT store, SUM(sales) FROM sales_data GROUP BY store HAVING SUM(sales) > 1000; -> Option C
  4. Quick Check:

    Filter groups by aggregate = HAVING [OK]
Hint: HAVING filters aggregates after GROUP BY [OK]
Common Mistakes:
  • Using WHERE with aggregate functions
  • Placing HAVING before GROUP BY
  • Filtering rows instead of groups
3. Given the table 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;
medium
A. Customers with total amount over 50
B. All customers with orders over 50 regardless of count
C. Syntax error because alias can't be used in HAVING
D. Customers with more than 2 orders where each order amount is over 50

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Customers with more than 2 orders where each order amount is over 50 -> Option D
  4. Quick Check:

    HAVING filters groups; alias usage depends on SQL dialect [OK]
Hint: Use full aggregate in HAVING or alias depending on SQL dialect [OK]
Common Mistakes:
  • Using alias in HAVING clause (may be allowed in some SQL dialects)
  • Confusing WHERE and HAVING filters
  • Assuming HAVING filters rows
4. Identify the error in this SQL query:
SELECT department, AVG(salary) FROM employees HAVING AVG(salary) > 50000 WHERE department LIKE 'Sales%' GROUP BY department;
medium
A. WHERE clause used after HAVING
B. HAVING clause used before GROUP BY
C. Missing alias for AVG(salary)
D. GROUP BY clause missing

Solution

  1. Step 1: Check SQL clause order

    The correct order is WHERE, then GROUP BY, then HAVING.
  2. Step 2: Identify misplaced WHERE clause

    In the query, WHERE appears after HAVING, which is invalid syntax.
  3. Final Answer:

    WHERE clause used after HAVING -> Option A
  4. Quick Check:

    WHERE before GROUP BY, HAVING after [OK]
Hint: WHERE before GROUP BY, HAVING after GROUP BY [OK]
Common Mistakes:
  • Placing WHERE after HAVING
  • Forgetting GROUP BY clause
  • Using HAVING without GROUP BY
5. You want to find all customers who placed more than 3 orders with each order amount greater than 100. Which query correctly applies WHERE and HAVING?
hard
A. SELECT customer_id FROM orders WHERE amount > 100 GROUP BY customer_id HAVING COUNT(*) > 3;
B. SELECT customer_id FROM orders GROUP BY customer_id HAVING COUNT(*) > 3 AND amount > 100;
C. SELECT customer_id FROM orders HAVING COUNT(*) > 3 WHERE amount > 100 GROUP BY customer_id;
D. SELECT customer_id FROM orders WHERE COUNT(*) > 3 GROUP BY customer_id HAVING amount > 100;

Solution

  1. Step 1: Filter rows with WHERE

    Use WHERE to keep only orders with amount > 100 before grouping.
  2. Step 2: Filter groups with HAVING

    Use HAVING to keep customers with more than 3 such orders (COUNT(*) > 3).
  3. Final Answer:

    SELECT customer_id FROM orders WHERE amount > 100 GROUP BY customer_id HAVING COUNT(*) > 3; -> Option A
  4. Quick Check:

    WHERE filters rows, HAVING filters groups [OK]
Hint: WHERE filters rows, HAVING filters groups after grouping [OK]
Common Mistakes:
  • Using HAVING to filter rows
  • Placing WHERE after HAVING
  • Using aggregate in WHERE clause