0
0
SQLquery~10 mins

Conditional aggregation pattern in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Conditional aggregation pattern
Start with data table
Apply condition inside aggregation
Aggregate values meeting condition
Return aggregated result
End
The flow shows how SQL applies a condition inside an aggregate function to summarize data selectively.
Execution Sample
SQL
SELECT
  COUNT(CASE WHEN status = 'active' THEN 1 END) AS active_count,
  SUM(CASE WHEN amount > 100 THEN amount ELSE 0 END) AS big_sales
FROM sales;
This query counts active statuses and sums sales amounts over 100 from the sales table.
Execution Table
StepRow DataCondition (status='active')Count ActionCondition (amount>100)Sum ActionRunning CountRunning Sum
1{status: 'active', amount: 150}TrueCount +1TrueSum +1501150
2{status: 'inactive', amount: 90}FalseCount +0FalseSum +01150
3{status: 'active', amount: 200}TrueCount +1TrueSum +2002350
4{status: 'active', amount: 50}TrueCount +1FalseSum +03350
5{status: 'inactive', amount: 300}FalseCount +0TrueSum +3003650
6{status: 'active', amount: 120}TrueCount +1TrueSum +1204770
ExitNo more rows----Final count=4Final sum=770
💡 All rows processed; aggregation complete.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
active_count01123344
big_sales0150150350350650770770
Key Moments - 2 Insights
Why does the count only increase when status is 'active'?
Because the COUNT uses CASE to include only rows where status='active' (see execution_table rows 1,3,4,6). Rows with other statuses add zero to count.
Why does the sum only add amounts greater than 100?
The SUM uses CASE to add amount only if amount>100, else adds 0 (see execution_table rows 1,3,5,6). Amounts 90 and 50 do not increase sum.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the active_count after processing row 4?
A3
B2
C4
D1
💡 Hint
Check the 'Running Count' column at step 4 in execution_table.
At which step does the big_sales sum first exceed 300?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Running Sum' column in execution_table to find when sum passes 300.
If the condition for counting changed to status='inactive', what would active_count be after step 6?
A3
B4
C2
D1
💡 Hint
Count how many rows have status='inactive' in the Row Data column.
Concept Snapshot
Conditional aggregation uses CASE inside aggregate functions.
Example: COUNT(CASE WHEN condition THEN 1 END) counts rows meeting condition.
SUM(CASE WHEN condition THEN value ELSE 0 END) sums values meeting condition.
This lets you summarize data selectively in one query.
Useful for counting or summing based on filters without WHERE.
Full Transcript
This visual execution shows how SQL performs conditional aggregation by applying conditions inside aggregate functions like COUNT and SUM. Each row is checked against conditions: if status equals 'active', the count increases by one; if amount is greater than 100, the sum adds that amount. The execution table tracks each row's data, condition results, and running totals. The variable tracker shows how active_count and big_sales change after each row. Key moments clarify why only certain rows affect counts or sums. The quiz tests understanding of running totals and condition effects. The snapshot summarizes the pattern: use CASE inside aggregates to count or sum conditionally in one query.