0
0
MySQLquery~20 mins

HAVING clause in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 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.