Challenge - 5 Problems
Conditional Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of conditional aggregation with CASE
Given a table Sales with columns
region and amount, what is the output of this query?SELECT region,
SUM(CASE WHEN amount > 100 THEN amount ELSE 0 END) AS high_sales
FROM Sales
GROUP BY region;
SQL
CREATE TABLE Sales (region VARCHAR(10), amount INT); INSERT INTO Sales VALUES ('East', 120), ('East', 80), ('West', 150), ('West', 90), ('North', 110); -- Query: SELECT region, SUM(CASE WHEN amount > 100 THEN amount ELSE 0 END) AS high_sales FROM Sales GROUP BY region;
Attempts:
2 left
💡 Hint
Think about how the CASE statement filters amounts greater than 100 before summing.
✗ Incorrect
The CASE inside SUM adds only amounts greater than 100. For East, only 120 counts; for West, only 150; for North, 110.
🧠 Conceptual
intermediate1:30remaining
Understanding conditional aggregation logic
Which of the following best describes what conditional aggregation does in SQL?
Attempts:
2 left
💡 Hint
Think about how CASE is used inside aggregate functions.
✗ Incorrect
Conditional aggregation uses CASE inside aggregates to include only certain values in the calculation, without filtering rows.
📝 Syntax
advanced2:00remaining
Identify the syntax error in conditional aggregation
Which option contains a syntax error in this conditional aggregation query?
SELECT department,
COUNT(CASE WHEN status = 'active' THEN 1 END) AS active_count
FROM Employees
GROUP BY department;
Attempts:
2 left
💡 Hint
Look carefully at the CASE syntax inside COUNT.
✗ Incorrect
Option A is missing the WHEN keyword after CASE, causing a syntax error.
❓ optimization
advanced2:30remaining
Optimizing conditional aggregation for performance
Which query is generally more efficient for counting rows with a condition in large tables?
Attempts:
2 left
💡 Hint
Consider how filtering rows before aggregation affects performance.
✗ Incorrect
Filtering rows in the WHERE clause (option C) reduces data before aggregation, usually improving performance over conditional aggregation inside aggregates.
🔧 Debug
expert3:00remaining
Debugging unexpected results in conditional aggregation
A developer wrote this query to count active and inactive users per city:
But the result shows zero counts for some cities that have users. What is the most likely cause?
SELECT city,
SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) AS active_users,
SUM(CASE WHEN status = 'inactive' THEN 1 ELSE 0 END) AS inactive_users
FROM Users
GROUP BY city;
But the result shows zero counts for some cities that have users. What is the most likely cause?
Attempts:
2 left
💡 Hint
Think about how NULL values affect conditional aggregation.
✗ Incorrect
If status is NULL, neither condition matches, so those rows add zero to both sums, possibly causing zero counts despite users existing.