FILTER clause for conditional aggregation in PostgreSQL - Time & Space Complexity
We want to understand how the time to run a query with a FILTER clause changes as the data grows.
Specifically, how does filtering inside aggregation affect performance?
Analyze the time complexity of the following code snippet.
SELECT department_id,
COUNT(*) FILTER (WHERE status = 'active') AS active_count,
COUNT(*) FILTER (WHERE status = 'inactive') AS inactive_count
FROM employees
GROUP BY department_id;
This query counts active and inactive employees per department using FILTER inside aggregation.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the employees table once.
- How many times: Once per row, checking conditions and grouping.
As the number of employees grows, the query checks each row once to decide which count to increase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time grows linearly with the number of rows in the table.
[X] Wrong: "Using FILTER makes the query run multiple times for each condition."
[OK] Correct: The database scans each row once and applies all FILTER conditions during that scan, so it does not repeat the whole query multiple times.
Understanding how conditional aggregation scales helps you write efficient queries and explain your reasoning clearly in interviews.
"What if we replaced FILTER with CASE statements inside COUNT? How would the time complexity change?"
Practice
FILTER clause do in a PostgreSQL aggregate function?Solution
Step 1: Understand the purpose of FILTER in aggregation
The FILTER clause allows you to specify a condition inside an aggregate function to include only rows that meet that condition.Step 2: Compare with other options
FILTER does not filter the whole table before aggregation, nor does it change grouping or sort results. It only limits rows counted or summed inside the aggregate.Final Answer:
It applies a condition to include only certain rows in the aggregation. -> Option BQuick Check:
FILTER = condition inside aggregate [OK]
- Thinking FILTER filters rows before aggregation
- Confusing FILTER with WHERE clause
- Assuming FILTER changes GROUP BY columns
status = 'active' using FILTER in PostgreSQL?Solution
Step 1: Recall FILTER syntax in aggregate functions
The correct syntax places FILTER with parentheses and a WHERE clause inside the aggregate function: COUNT(*) FILTER (WHERE condition).Step 2: Check each option
SELECT COUNT(*) FILTER (WHERE status = 'active') FROM users; matches the correct syntax. The other options misuse FILTER placement or syntax.Final Answer:
SELECT COUNT(*) FILTER (WHERE status = 'active') FROM users; -> Option AQuick Check:
FILTER syntax = FILTER (WHERE condition) [OK]
- Omitting parentheses after FILTER
- Placing FILTER outside aggregate function
- Using FILTER without WHERE keyword
sales with columns region and amount, what will this query return?SELECT region, SUM(amount) FILTER (WHERE amount > 100) AS big_sales FROM sales GROUP BY region;
Solution
Step 1: Understand the FILTER condition in SUM
The FILTER clause limits the SUM to only rows where amount > 100.Step 2: Analyze the GROUP BY effect
The query groups rows by region, then sums only amounts above 100 in each group.Final Answer:
Sum of sales amounts greater than 100 per region. -> Option DQuick Check:
SUM with FILTER (WHERE amount > 100) = sum of big sales [OK]
- Thinking it sums all amounts, ignoring FILTER
- Confusing SUM with COUNT
- Ignoring GROUP BY effect
SELECT department, COUNT(*) FILTER (status = 'active') FROM employees GROUP BY department;
Solution
Step 1: Check FILTER clause syntax
FILTER requires a WHERE keyword before the condition: FILTER (WHERE condition).Step 2: Verify other parts
GROUP BY on department is correct. COUNT(*) works with FILTER. FILTER must be inside aggregate function.Final Answer:
Missing WHERE keyword inside FILTER clause. -> Option AQuick Check:
FILTER syntax needs WHERE [OK]
- Omitting WHERE in FILTER
- Placing FILTER outside aggregate
- Adding unnecessary columns to GROUP BY
Solution
Step 1: Identify total sales calculation
Total sales is the sum of all amounts per store, so SUM(amount) without filter is correct.Step 2: Identify big sales calculation
Big sales are sums of amounts greater than 500, so SUM(amount) FILTER (WHERE amount > 500) is correct.Step 3: Check options
SELECT store, SUM(amount) AS total_sales, SUM(amount) FILTER (WHERE amount > 500) AS big_sales FROM sales GROUP BY store; correctly assigns total_sales and big_sales. The other options mix sums and counts incorrectly or swap columns.Final Answer:
SELECT store, SUM(amount) AS total_sales, SUM(amount) FILTER (WHERE amount > 500) AS big_sales FROM sales GROUP BY store; -> Option CQuick Check:
SUM with and without FILTER for totals [OK]
- Swapping filtered and unfiltered sums
- Using COUNT instead of SUM for amounts
- Misplacing FILTER clause
Ready to prove it?
Pass the test and this pattern moves unknown
