What is the conditional aggregation pattern in SQL?
It is a way to calculate sums, counts, or other aggregates based on conditions inside a single query, using CASE statements inside aggregate functions.
Click to reveal answer
beginner
How do you count rows that meet a condition using conditional aggregation?
Use COUNT(CASE WHEN condition THEN 1 END) or SUM(CASE WHEN condition THEN 1 ELSE 0 END) to count only rows that satisfy the condition.
Click to reveal answer
intermediate
Why use conditional aggregation instead of multiple queries?
It combines multiple conditional counts or sums into one query, making it faster and easier to read, like counting different categories in one go.
Click to reveal answer
beginner
Write a simple example of conditional aggregation to count male and female employees.
SELECT
COUNT(CASE WHEN gender = 'M' THEN 1 END) AS male_count,
COUNT(CASE WHEN gender = 'F' THEN 1 END) AS female_count
FROM employees;
Click to reveal answer
beginner
What happens if the CASE condition is false in conditional aggregation?
The CASE returns NULL or 0 (if ELSE 0 is used), so the aggregate function ignores or does not add that row to the count or sum.
Click to reveal answer
Which SQL function is commonly used with CASE for conditional aggregation?
ASUM
BJOIN
CORDER BY
DGROUP BY
✗ Incorrect
SUM is often combined with CASE to add values conditionally in one query.
How do you count rows where a condition is true using conditional aggregation?
AWHERE condition
BCOUNT(CASE WHEN condition THEN 1 END)
CGROUP BY condition
DORDER BY condition
✗ Incorrect
COUNT with CASE counts only rows where the condition is true.
What does this SQL snippet do? SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END)
ASums all rows
BCounts inactive rows
CCounts active rows
DFilters rows
✗ Incorrect
It sums 1 for each active row, effectively counting them.
Why is conditional aggregation useful?
ATo combine multiple conditional counts or sums in one query
BTo delete rows
CTo create new tables
DTo sort data
✗ Incorrect
It helps summarize data by different conditions efficiently.
What will happen if CASE returns NULL inside an aggregate function like COUNT?
AThe query fails
BNULL is counted as zero
CThe row is counted anyway
DThat row is ignored in the count
✗ Incorrect
COUNT ignores NULL values, so those rows are not counted.
Explain how to use the conditional aggregation pattern to count different categories in one SQL query.
Think about how CASE can return values only when conditions are met.
You got /4 concepts.
Describe the difference between filtering rows with WHERE and counting conditionally with CASE inside aggregation.
Consider when rows are included or excluded in each method.
You got /3 concepts.
Practice
(1/5)
1. What does conditional aggregation in SQL allow you to do? SUM(CASE WHEN condition THEN value ELSE 0 END)
easy
A. Delete rows that meet a condition
B. Join two tables based on a condition
C. Create a new table from existing data
D. Calculate sums or counts based on specific conditions within one query
Hint: Think: aggregate + CASE filters rows inside one query [OK]
Common Mistakes:
Confusing conditional aggregation with JOIN operations
Thinking it creates or deletes tables
Assuming it filters rows outside aggregation
2. Which of the following is the correct syntax using COUNT for counting only rows where status = 'active' using conditional aggregation?
easy
A. COUNT(CASE WHEN status = 'active' THEN 1 ELSE NULL END)
B. COUNT(CASE status = 'active' THEN 1 END)
C. SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END)
D. COUNT(IF status = 'active' THEN 1 ELSE 0 END)
Solution
Step 1: Recall correct CASE syntax inside COUNT
The CASE expression must have WHEN condition THEN value ELSE NULL END for COUNT to count only matching rows.
Step 2: Check each option
COUNT(CASE WHEN status = 'active' THEN 1 ELSE NULL END) uses correct CASE syntax with ELSE NULL, so COUNT counts only 'active' rows. COUNT(CASE status = 'active' THEN 1 END) misses WHEN keyword. SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) uses SUM, not COUNT. COUNT(IF status = 'active' THEN 1 ELSE 0 END) uses invalid IF syntax.
Final Answer:
COUNT(CASE WHEN status = 'active' THEN 1 ELSE NULL END) -> Option A
Quick Check:
COUNT + CASE WHEN ... THEN 1 ELSE NULL END [OK]
Hint: Use ELSE NULL inside CASE for COUNT to skip rows [OK]
Common Mistakes:
Omitting WHEN keyword in CASE
Using SUM instead of COUNT for counting rows
Using ELSE 0 which counts zeros as rows
3. Given the 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 big_sales FROM sales GROUP BY region;
medium
A. Sum of sales amounts greater than 100 per region
B. Count of sales with amount greater than 100 per region
C. Sum of all sales amounts per region
D. Sum of sales amounts less than or equal to 100 per region
Solution
Step 1: Analyze the CASE inside SUM
The CASE returns amount if amount > 100, else 0. So only amounts above 100 contribute to the sum.
Step 2: Understand GROUP BY effect
The query groups rows by region, summing only big sales (amount > 100) per region.
Final Answer:
Sum of sales amounts greater than 100 per region -> Option A
Quick Check:
SUM with CASE filters amounts > 100 [OK]
Hint: CASE filters values inside SUM to include only big sales [OK]
Common Mistakes:
Thinking it sums all sales, ignoring condition
Confusing SUM with COUNT
Assuming it sums small sales instead
4. Identify the error in this SQL query using conditional aggregation:
SELECT department, COUNT(CASE WHEN salary > 50000 THEN 1 ELSE 0 END) AS high_earners FROM employees GROUP BY department;
medium
A. SUM should be used instead of COUNT
B. COUNT counts zeros, so it counts all rows, not just high earners
C. CASE syntax is invalid
D. Missing GROUP BY clause
Solution
Step 1: Understand COUNT behavior with CASE
COUNT counts all non-NULL values. Here ELSE 0 returns zero, which is counted as a value, so all rows count.
Step 2: Identify correct fix
To count only high earners, ELSE should be NULL so those rows are ignored by COUNT.
Final Answer:
COUNT counts zeros, so it counts all rows, not just high earners -> Option B
Quick Check:
COUNT counts non-NULL, zero is counted [OK]
Hint: Use ELSE NULL to exclude rows from COUNT [OK]
Common Mistakes:
Using ELSE 0 causes wrong counts
Confusing COUNT with SUM
Ignoring that zero is counted by COUNT
5. You want to find the total sales and the count of sales above 1000 for each product in one query. Which SQL query correctly uses conditional aggregation?
hard
A. SELECT product, COUNT(amount) AS total_sales, SUM(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) AS big_sales_count FROM sales GROUP BY product;
B. SELECT product, SUM(CASE WHEN amount > 1000 THEN amount ELSE 0 END) AS total_sales, COUNT(amount) AS big_sales_count FROM sales GROUP BY product;
C. SELECT product, SUM(amount) AS total_sales, SUM(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) AS big_sales_count FROM sales GROUP BY product;
D. SELECT product, SUM(amount) AS total_sales, COUNT(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) AS big_sales_count FROM sales GROUP BY product;
Solution
Step 1: Identify total sales calculation
Total sales is sum of all amounts, so SUM(amount) is correct.
Step 2: Identify count of sales above 1000
Counting sales above 1000 can be done by summing 1 for each qualifying row and 0 otherwise, so SUM(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) works.
Step 3: Check each option
SELECT product, SUM(amount) AS total_sales, COUNT(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) AS big_sales_count FROM sales GROUP BY product; uses COUNT with ELSE 0 which counts all rows incorrectly; SELECT product, SUM(CASE WHEN amount > 1000 THEN amount ELSE 0 END) AS total_sales, COUNT(amount) AS big_sales_count FROM sales GROUP BY product; incorrectly sums only amounts above 1000 for total_sales; SELECT product, COUNT(amount) AS total_sales, SUM(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) AS big_sales_count FROM sales GROUP BY product; swaps SUM and COUNT incorrectly; SELECT product, SUM(amount) AS total_sales, SUM(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) AS big_sales_count FROM sales GROUP BY product; correctly sums total sales and counts big sales using SUM with CASE.
Final Answer:
SELECT product, SUM(amount) AS total_sales, SUM(CASE WHEN amount > 1000 THEN 1 ELSE 0 END) AS big_sales_count FROM sales GROUP BY product; -> Option C
Quick Check:
SUM for totals, SUM(CASE) for conditional counts [OK]
Hint: Use SUM(CASE) for conditional counts, SUM(column) for totals [OK]
Common Mistakes:
Using COUNT with ELSE 0 which counts all rows
Summing only filtered amounts for total sales
Mixing COUNT and SUM incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.