0
0
SQLquery~5 mins

CASE with aggregate functions in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the CASE statement do when used with aggregate functions in SQL?
It allows you to apply conditional logic inside aggregate functions, so you can count or sum values based on specific conditions.
Click to reveal answer
beginner
Write a simple example of using CASE inside a COUNT aggregate function.
Example: <br>SELECT COUNT(CASE WHEN status = 'active' THEN 1 END) AS active_count FROM users;<br>This counts only rows where status is 'active'.
Click to reveal answer
intermediate
Why use CASE inside SUM or COUNT instead of filtering with WHERE?
CASE lets you count or sum different conditions in the same query without filtering out rows, so you get multiple results in one query.
Click to reveal answer
intermediate
How does the CASE statement handle ELSE in aggregate functions?
ELSE defines what value to use when no conditions match. If ELSE is omitted, NULL is used, which aggregate functions usually ignore.
Click to reveal answer
intermediate
Can you use multiple WHEN conditions inside a single CASE for aggregate functions?
Yes, you can have multiple WHEN conditions to handle different cases and assign values accordingly inside aggregate functions.
Click to reveal answer
What does this SQL do? SELECT SUM(CASE WHEN score > 50 THEN 1 ELSE 0 END) FROM tests;
ASums all scores
BSums all scores above 50
CCounts all scores below 50
DCounts how many scores are above 50
Which aggregate function ignores NULL values by default?
ACOUNT
BAVG
CAll of the above
DSUM
What happens if you omit ELSE in a CASE inside an aggregate function?
AIt treats unmatched cases as NULL
BIt causes an error
CIt treats unmatched cases as 0
DIt treats unmatched cases as 1
Why use CASE inside aggregate functions instead of WHERE clause?
ATo filter rows before aggregation
BTo apply conditions inside aggregation and get multiple results in one query
CTo speed up the query
DTo join tables
Which is a valid use of CASE with COUNT?
ACOUNT(CASE WHEN age > 18 THEN 1 END)
BCOUNT(CASE age > 18 THEN 1 END)
CCOUNT(CASE WHEN age > 18 THEN END)
DCOUNT(CASE age > 18)
Explain how to use CASE inside aggregate functions to count conditional values in SQL.
Think about counting only rows that meet a condition without filtering the whole query.
You got /5 concepts.
    Describe the difference between filtering rows with WHERE and using CASE inside aggregate functions.
    Consider when you want multiple counts or sums in one query versus just filtering rows.
    You got /5 concepts.