Bird
Raised Fist0
MySQLquery~20 mins

HAVING clause in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
HAVING Clause Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of HAVING with GROUP BY and aggregate
Given the table sales with columns region and amount, what is the output of this query?
SELECT region, SUM(amount) AS total_sales
FROM sales
GROUP BY region
HAVING total_sales > 1000;
MySQL
SELECT region, SUM(amount) AS total_sales
FROM sales
GROUP BY region
HAVING total_sales > 1000;
ASyntax error because HAVING cannot use alias
BRows with regions where the average amount is more than 1000
CRows with regions where the sum of amount is more than 1000
DAll rows from sales table without filtering
Attempts:
2 left
💡 Hint
HAVING filters groups after aggregation, using the alias defined in SELECT.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in HAVING clause
Which option shows a syntax error in using HAVING clause in MySQL?
ASELECT department, COUNT(*) FROM employees WHERE COUNT(*) > 5;
BSELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5;
CSELECT department, COUNT(*) FROM employees GROUP BY department HAVING department = 'Sales';
DSELECT department, COUNT(*) FROM employees HAVING COUNT(*) > 5;
Attempts:
2 left
💡 Hint
WHERE cannot filter on aggregate functions.
🧠 Conceptual
advanced
2:00remaining
Difference between WHERE and HAVING clauses
Which statement correctly explains the difference between WHERE and HAVING clauses in SQL?
AWHERE filters rows before grouping; HAVING filters groups after aggregation.
BWHERE filters groups after aggregation; HAVING filters rows before grouping.
CWHERE and HAVING both filter rows before grouping but HAVING is slower.
DWHERE filters only numeric columns; HAVING filters only string columns.
Attempts:
2 left
💡 Hint
Think about when filtering happens in the query process.
❓ query_result
advanced
2:00remaining
Output of HAVING with multiple conditions
Consider the table orders with columns customer_id, order_date, and total. What rows will this query return?
SELECT customer_id, COUNT(*) AS order_count, SUM(total) AS total_spent
FROM orders
GROUP BY customer_id
HAVING order_count >= 3 AND total_spent < 500;
MySQL
SELECT customer_id, COUNT(*) AS order_count, SUM(total) AS total_spent
FROM orders
GROUP BY customer_id
HAVING order_count >= 3 AND total_spent < 500;
AAll customers regardless of orders or spending
BCustomers with at least 3 orders and total spending less than 500
CCustomers with exactly 3 orders or total spending less than 500
DSyntax error due to multiple conditions in HAVING
Attempts:
2 left
💡 Hint
HAVING can combine multiple conditions with AND/OR.
🔧 Debug
expert
2:00remaining
Why does this HAVING query return no rows?
Given the table products with columns category and price, why does this query return no rows?
SELECT category, AVG(price) AS avg_price
FROM products
GROUP BY category
HAVING avg_price < 0;
MySQL
SELECT category, AVG(price) AS avg_price
FROM products
GROUP BY category
HAVING avg_price < 0;
ABecause GROUP BY is missing a WHERE clause to filter rows
BBecause HAVING cannot use aliases like avg_price
CBecause AVG(price) returns NULL for all categories
DBecause average price cannot be less than zero, so no groups match the condition
Attempts:
2 left
💡 Hint
Think about the possible values of average price.

Practice

(1/5)
1. What is the main purpose of the HAVING clause in SQL?
easy
A. To filter groups after aggregation
B. To filter rows before grouping
C. To sort the result set
D. To join two tables

Solution

  1. Step 1: Understand filtering in SQL

    The WHERE clause filters rows before grouping, but HAVING filters after groups are formed.
  2. Step 2: Identify the role of HAVING

    HAVING is used to filter groups based on aggregate conditions like COUNT or SUM.
  3. Final Answer:

    To filter groups after aggregation -> Option A
  4. Quick Check:

    HAVING filters groups, not rows [OK]
Hint: HAVING filters groups after aggregation, WHERE filters rows before [OK]
Common Mistakes:
  • Confusing HAVING with WHERE clause
  • Using HAVING without GROUP BY
  • Thinking HAVING sorts data
2. Which of the following is the correct syntax to use HAVING with GROUP BY in MySQL?
easy
A. SELECT col1 FROM table HAVING COUNT(col2) > 5 GROUP BY col1;
B. SELECT col1, COUNT(col2) FROM table GROUP BY col1 HAVING COUNT(col2) > 5;
C. SELECT col1, COUNT(col2) FROM table HAVING COUNT(col2) > 5;
D. SELECT col1, COUNT(col2) FROM table GROUP BY col1 WHERE COUNT(col2) > 5;

Solution

  1. Step 1: Recall correct order of clauses

    In SQL, GROUP BY comes before HAVING. The correct order is SELECT -> FROM -> GROUP BY -> HAVING.
  2. Step 2: Check each option's syntax

    SELECT col1, COUNT(col2) FROM table GROUP BY col1 HAVING COUNT(col2) > 5; correctly places GROUP BY before HAVING and includes aggregate function in HAVING condition.
  3. Final Answer:

    SELECT col1, COUNT(col2) FROM table GROUP BY col1 HAVING COUNT(col2) > 5; -> Option B
  4. Quick Check:

    GROUP BY before HAVING [OK]
Hint: GROUP BY must come before HAVING in SQL syntax [OK]
Common Mistakes:
  • Placing HAVING before GROUP BY
  • Using WHERE with aggregate functions
  • Omitting GROUP BY when using HAVING
3. Given the table sales with columns region and amount, what will this query return?
SELECT region, SUM(amount) AS total_sales FROM sales GROUP BY region HAVING total_sales > 1000;
medium
A. All regions with sales less than or equal to 1000
B. Syntax error because alias can't be used in HAVING
C. All regions with total sales greater than 1000
D. Empty result because HAVING filters rows

Solution

  1. Step 1: Understand the query logic

    The query groups rows by region and sums amount as total_sales.
  2. Step 2: Apply HAVING condition

    It filters groups where total_sales is greater than 1000. Using alias in HAVING is allowed in MySQL.
  3. Final Answer:

    All regions with total sales greater than 1000 -> Option C
  4. Quick Check:

    HAVING filters groups by aggregate result [OK]
Hint: HAVING filters groups by aggregate values like SUM [OK]
Common Mistakes:
  • Thinking HAVING filters rows, not groups
  • Assuming alias can't be used in HAVING in MySQL
  • Confusing HAVING with WHERE
4. Identify the error in this query:
SELECT department, COUNT(*) FROM employees HAVING COUNT(*) > 10;
medium
A. Missing GROUP BY clause before HAVING
B. COUNT(*) cannot be used in HAVING
C. HAVING should be replaced with WHERE
D. No error, query is correct

Solution

  1. Step 1: Check for GROUP BY usage

    The query uses HAVING with COUNT(*) but does not group by department. HAVING requires GROUP BY to define groups.
  2. Step 2: Understand HAVING requirements

    HAVING filters groups after aggregation. Without GROUP BY, groups are not defined, causing an error.
  3. Final Answer:

    Missing GROUP BY clause before HAVING -> Option A
  4. Quick Check:

    HAVING needs GROUP BY to filter groups [OK]
Hint: Always use GROUP BY before HAVING to define groups [OK]
Common Mistakes:
  • Using HAVING without GROUP BY
  • Replacing HAVING with WHERE incorrectly
  • Assuming COUNT(*) can't be in HAVING
5. You have a products table with columns category, price, and stock. Write a query to find categories where the average price is above 50 and total stock is at least 100.
Which query correctly uses HAVING to achieve this?
hard
A. SELECT category FROM products GROUP BY category WHERE AVG(price) > 50 AND SUM(stock) >= 100;
B. SELECT category FROM products HAVING AVG(price) > 50 AND SUM(stock) >= 100 GROUP BY category;
C. SELECT category FROM products WHERE AVG(price) > 50 AND SUM(stock) >= 100 GROUP BY category;
D. SELECT category FROM products GROUP BY category HAVING AVG(price) > 50 AND SUM(stock) >= 100;

Solution

  1. Step 1: Recall correct clause order

    The correct order is SELECT -> FROM -> GROUP BY -> HAVING. HAVING filters groups based on aggregate conditions.
  2. Step 2: Analyze each option

    SELECT category FROM products GROUP BY category HAVING AVG(price) > 50 AND SUM(stock) >= 100; correctly groups by category and uses HAVING with aggregate functions AVG(price) and SUM(stock).
  3. Final Answer:

    SELECT category FROM products GROUP BY category HAVING AVG(price) > 50 AND SUM(stock) >= 100; -> Option D
  4. Quick Check:

    HAVING filters grouped data with aggregate conditions [OK]
Hint: Use HAVING after GROUP BY for aggregate filters [OK]
Common Mistakes:
  • Placing HAVING before GROUP BY
  • Using WHERE with aggregate functions
  • Omitting GROUP BY when using HAVING
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard