0
0
PostgreSQLquery~10 mins

FILTER clause for conditional aggregation in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - FILTER clause for conditional aggregation
Start Query
GROUP BY rows
Apply aggregate functions
Check FILTER condition for each aggregate
Include row
Calculate aggregate result
Return grouped results
The query groups rows, then for each aggregate function, it includes only rows that meet the FILTER condition before calculating the aggregate.
Execution Sample
PostgreSQL
SELECT department,
       COUNT(*) FILTER (WHERE salary > 50000) AS high_earners,
       COUNT(*) AS total_employees
FROM employees
GROUP BY department;
Counts employees per department, counting only those with salary > 50000 as high earners.
Execution Table
StepRow Data (department, salary)AggregateFILTER Condition Met?ActionAggregate State
1(Sales, 60000)COUNT(*) FILTER (WHERE salary > 50000)YesInclude in high_earners counthigh_earners=1
1(Sales, 60000)COUNT(*)N/AInclude in total_employees counttotal_employees=1
2(Sales, 45000)COUNT(*) FILTER (WHERE salary > 50000)NoExclude from high_earners counthigh_earners=1
2(Sales, 45000)COUNT(*)N/AInclude in total_employees counttotal_employees=2
3(HR, 52000)COUNT(*) FILTER (WHERE salary > 50000)YesInclude in high_earners counthigh_earners=1
3(HR, 52000)COUNT(*)N/AInclude in total_employees counttotal_employees=1
4(HR, 48000)COUNT(*) FILTER (WHERE salary > 50000)NoExclude from high_earners counthigh_earners=1
4(HR, 48000)COUNT(*)N/AInclude in total_employees counttotal_employees=2
5(IT, 70000)COUNT(*) FILTER (WHERE salary > 50000)YesInclude in high_earners counthigh_earners=1
5(IT, 70000)COUNT(*)N/AInclude in total_employees counttotal_employees=1
6(IT, 30000)COUNT(*) FILTER (WHERE salary > 50000)NoExclude from high_earners counthigh_earners=1
6(IT, 30000)COUNT(*)N/AInclude in total_employees counttotal_employees=2
7Group Sales completeFinal countsN/AOutput: high_earners=1, total_employees=2
8Group HR completeFinal countsN/AOutput: high_earners=1, total_employees=2
9Group IT completeFinal countsN/AOutput: high_earners=1, total_employees=2
10Query completeAll groups processedN/AReturn final result set
💡 All rows processed and aggregates computed per group.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5After Row 6Final
high_earners (Sales)0111
total_employees (Sales)0122
high_earners (HR)0111
total_employees (HR)0122
high_earners (IT)0111
total_employees (IT)0122
Key Moments - 3 Insights
Why does the FILTER clause exclude some rows from the aggregate?
Because FILTER applies a condition to decide if a row counts for that aggregate. Rows not meeting the condition are ignored for that aggregate but still counted in others, as shown in execution_table rows 2 and 4.
Does FILTER affect the GROUP BY operation?
No, FILTER only affects which rows are counted inside an aggregate function after grouping. Grouping happens first, then FILTER decides which rows inside each group to include.
Can FILTER be used with any aggregate function?
Yes, FILTER works with aggregates like COUNT, SUM, AVG, etc., to conditionally include rows, as demonstrated with COUNT in the example.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2 for the Sales row with salary 45000. Does this row count towards high_earners?
ANo, it is excluded
BYes, it is included
COnly partially included
DIt depends on other rows
💡 Hint
Check the 'FILTER Condition Met?' column at Step 2 for that row.
At which step does the total_employees count for Sales reach 2?
AAfter Step 1
BAfter Step 2
CAfter Step 3
DAfter Step 4
💡 Hint
Look at the 'Aggregate State' for total_employees in Sales rows.
If the FILTER condition changed to salary > 60000, how would the high_earners count for IT change?
AIt would decrease to 0
BIt would increase
CIt would stay the same
DIt would count all employees
💡 Hint
Refer to variable_tracker for high_earners (IT) and consider which salaries meet the new condition.
Concept Snapshot
FILTER clause syntax:
AGG_FUNC(...) FILTER (WHERE condition)

Behavior:
- Applies condition inside aggregate
- Only rows meeting condition count
- Other rows ignored for that aggregate

Key rule:
FILTER does not affect grouping, only aggregation.
Full Transcript
This visual execution shows how the FILTER clause works in PostgreSQL aggregation. The query groups employees by department. For each group, it counts total employees and counts only those with salary over 50000 as high earners. Each row is checked against the FILTER condition before counting in the filtered aggregate. Rows not meeting the condition are excluded from that aggregate but still counted in others. The execution table traces each row's effect on aggregates step-by-step. The variable tracker shows how counts update per group. Key moments clarify common confusions about FILTER's role and scope. The quiz tests understanding by referencing specific steps and variable states.