0
0
SQLquery~20 mins

HAVING clause for filtering groups in SQL - 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 COUNT filter

Consider a table Orders with columns CustomerID and OrderID. What is the output of this query?

SELECT CustomerID, COUNT(OrderID) AS OrderCount
FROM Orders
GROUP BY CustomerID
HAVING COUNT(OrderID) > 2;

Assume the table has these rows:

CustomerID | OrderID
-----------|--------
1          | 101
1          | 102
2          | 103
2          | 104
2          | 105
3          | 106
SQL
SELECT CustomerID, COUNT(OrderID) AS OrderCount
FROM Orders
GROUP BY CustomerID
HAVING COUNT(OrderID) > 2;
A[{"CustomerID": 2, "OrderCount": 3}]
B[{"CustomerID": 3, "OrderCount": 1}]
C[{"CustomerID": 1, "OrderCount": 2}, {"CustomerID": 2, "OrderCount": 3}]
D[]
Attempts:
2 left
💡 Hint

Remember, HAVING filters groups after aggregation.

🧠 Conceptual
intermediate
1:00remaining
Purpose of HAVING clause

What is the main purpose of the HAVING clause in SQL?

ATo filter groups after aggregation
BTo filter rows before grouping
CTo sort the result set
DTo join two tables
Attempts:
2 left
💡 Hint

Think about when filtering happens in relation to grouping.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in HAVING usage

Which option contains a syntax error in using the HAVING clause?

SELECT Department, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY Department
HAVING Salary > 50000;
AHAVING SUM(Salary) > 100000;
BHAVING AVG(Salary) > 50000;
CHAVING COUNT(*) > 5;
DHAVING Salary > 50000;
Attempts:
2 left
💡 Hint

Remember what HAVING can filter on after grouping.

optimization
advanced
2:00remaining
Optimizing HAVING with WHERE

Given a large Sales table with columns Region, Amount, and SaleDate, which query is more efficient to find regions with total sales over 10000 in 2023?

ASELECT Region, SUM(Amount) FROM Sales HAVING SUM(Amount) > 10000;
BSELECT Region, SUM(Amount) FROM Sales GROUP BY Region HAVING SUM(Amount) > 10000 AND YEAR(SaleDate) = 2023;
CSELECT Region, SUM(Amount) FROM Sales WHERE YEAR(SaleDate) = 2023 GROUP BY Region HAVING SUM(Amount) > 10000;
DSELECT Region, SUM(Amount) FROM Sales WHERE SUM(Amount) > 10000 GROUP BY Region;
Attempts:
2 left
💡 Hint

Filter rows before grouping when possible.

🔧 Debug
expert
2:30remaining
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 AvgPrice
FROM Products
GROUP BY Category
HAVING AVG(Price) < 0;
ABecause AVG(Price) returns NULL for all groups.
BBecause average price cannot be less than zero, so no groups match the condition.
CBecause the HAVING clause syntax is incorrect.
DBecause the GROUP BY clause is missing a column.
Attempts:
2 left
💡 Hint

Think about the possible values of average price.