0
0
SQLquery~20 mins

Conditional aggregation pattern in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[{"region": "East", "high_sales": 200}, {"region": "West", "high_sales": 240}, {"region": "North", "high_sales": 110}]
B[{"region": "East", "high_sales": 120}, {"region": "West", "high_sales": 150}, {"region": "North", "high_sales": 110}]
C[{"region": "East", "high_sales": 80}, {"region": "West", "high_sales": 90}, {"region": "North", "high_sales": 0}]
D[{"region": "East", "high_sales": 200}, {"region": "West", "high_sales": 150}, {"region": "North", "high_sales": 110}]
Attempts:
2 left
💡 Hint
Think about how the CASE statement filters amounts greater than 100 before summing.
🧠 Conceptual
intermediate
1:30remaining
Understanding conditional aggregation logic
Which of the following best describes what conditional aggregation does in SQL?
AIt applies a condition inside an aggregate function to selectively include values.
BIt changes the grouping columns dynamically based on conditions.
CIt filters rows before aggregation based on a condition.
DIt creates new tables with aggregated data conditionally.
Attempts:
2 left
💡 Hint
Think about how CASE is used inside aggregate functions.
📝 Syntax
advanced
2: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;
ASELECT department, COUNT(CASE status = 'active' THEN 1 END) AS active_count FROM Employees GROUP BY department;
BSELECT department, COUNT(CASE WHEN status = 'active' THEN 1 END) AS active_count FROM Employees GROUP BY department;
CSELECT department, COUNT(CASE WHEN status = 'active' THEN 1 ELSE 0 END) AS active_count FROM Employees GROUP BY department;
DSELECT department, COUNT(CASE WHEN status = 'active' THEN 1 ELSE NULL END) AS active_count FROM Employees GROUP BY department;
Attempts:
2 left
💡 Hint
Look carefully at the CASE syntax inside COUNT.
optimization
advanced
2:30remaining
Optimizing conditional aggregation for performance
Which query is generally more efficient for counting rows with a condition in large tables?
ASELECT COUNT(*) FILTER (WHERE status = 'active') FROM Employees;
BSELECT SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) FROM Employees;
CSELECT COUNT(*) FROM Employees WHERE status = 'active';
DSELECT COUNT(CASE WHEN status = 'active' THEN 1 ELSE NULL END) FROM Employees;
Attempts:
2 left
💡 Hint
Consider how filtering rows before aggregation affects performance.
🔧 Debug
expert
3:00remaining
Debugging unexpected results in conditional aggregation
A developer wrote this query to count active and inactive users per city:
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?
AThe SUM function cannot be used with CASE statements.
BThe ELSE 0 in CASE causes all counts to be zero.
CThe GROUP BY city is missing a HAVING clause to filter cities.
DThe status column contains NULL values that are not counted in either condition.
Attempts:
2 left
💡 Hint
Think about how NULL values affect conditional aggregation.