0
0
PostgreSQLquery~20 mins

FILTER clause for conditional aggregation in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FILTER Clause Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Calculate conditional sums using FILTER clause
Given a table sales with columns region, amount, and category, what is the output of this query?
SELECT region,
SUM(amount) FILTER (WHERE category = 'A') AS sum_a,
SUM(amount) FILTER (WHERE category = 'B') AS sum_b
FROM sales
GROUP BY region
ORDER BY region;

Assume the table data is:
region | amount | category
-------+--------+---------
East   | 100    | A
East   | 200    | B
West   | 150    | A
West   | 50     | B
West   | 100    | C
A[{"region": "East", "sum_a": 100, "sum_b": 200}, {"region": "West", "sum_a": 150, "sum_b": 50}]
B[{"region": "East", "sum_a": 100, "sum_b": 200}, {"region": "West", "sum_a": 150, "sum_b": 0}]
C[{"region": "East", "sum_a": 100, "sum_b": 0}, {"region": "West", "sum_a": 150, "sum_b": 50}]
D[{"region": "East", "sum_a": 300, "sum_b": 200}, {"region": "West", "sum_a": 150, "sum_b": 150}]
Attempts:
2 left
💡 Hint
Remember FILTER applies the condition only to the aggregation inside it.
🧠 Conceptual
intermediate
1:30remaining
Understanding FILTER clause behavior with NULL values
Consider a table orders with columns customer_id, status, and total. What does the FILTER clause do when the condition evaluates to NULL?
SELECT customer_id,
COUNT(*) FILTER (WHERE status = 'completed') AS completed_orders
FROM orders
GROUP BY customer_id;

If some rows have status as NULL, how are they counted?
ARows with NULL status are counted in completed_orders because NULL is treated as true.
BRows with NULL status are not counted in completed_orders because the condition evaluates to NULL, which is treated as false.
CRows with NULL status cause the query to fail with an error.
DRows with NULL status are counted as zero but included in the total count.
Attempts:
2 left
💡 Hint
FILTER counts only rows where the condition is true, NULL is not true.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in FILTER clause usage
Which of the following queries will cause a syntax error in PostgreSQL?
SELECT department,
SUM(salary) FILTER (category = 'Engineering')
FROM employees
GROUP BY department;
ASyntax error because GROUP BY is missing a column.
BThe query runs without error and returns sums filtered by category.
CSyntax error because SUM cannot be used with FILTER clause.
DSyntax error because FILTER clause requires WHERE keyword before the condition.
Attempts:
2 left
💡 Hint
Check the FILTER clause syntax carefully.
optimization
advanced
2:00remaining
Optimize query using FILTER clause instead of CASE
You have this query:
SELECT region,
SUM(CASE WHEN category = 'A' THEN amount ELSE 0 END) AS sum_a,
SUM(CASE WHEN category = 'B' THEN amount ELSE 0 END) AS sum_b
FROM sales
GROUP BY region;

Which rewritten query using FILTER clause is equivalent and potentially more efficient?
ASELECT region, SUM(amount) FILTER (WHERE category = 'A') AS sum_a, SUM(amount) FILTER (WHERE category = 'B') AS sum_b FROM sales GROUP BY region;
BSELECT region, SUM(amount) WHERE category = 'A' AS sum_a, SUM(amount) WHERE category = 'B' AS sum_b FROM sales GROUP BY region;
CSELECT region, SUM(amount) FILTER (category = 'A') AS sum_a, SUM(amount) FILTER (category = 'B') AS sum_b FROM sales GROUP BY region;
DSELECT region, SUM(amount) CASE WHEN category = 'A' THEN amount END AS sum_a, SUM(amount) CASE WHEN category = 'B' THEN amount END AS sum_b FROM sales GROUP BY region;
Attempts:
2 left
💡 Hint
FILTER clause requires WHERE keyword and condition inside parentheses.
🔧 Debug
expert
2:30remaining
Debug unexpected results with FILTER clause and NULLs
A developer wrote this query:
SELECT product_id,
COUNT(*) FILTER (WHERE status = 'sold') AS sold_count,
COUNT(*) FILTER (WHERE status != 'sold') AS other_count
FROM product_sales
GROUP BY product_id;

But the other_count is unexpectedly low. The status column contains NULL values. Why?
Choose the best explanation.
AThe query fails because NULL values cause COUNT(*) to error inside FILTER.
BThe query counts NULLs in other_count because NULL != 'sold' is true.
CThe condition <code>status != 'sold'</code> excludes NULLs because NULL comparison returns NULL, so those rows are not counted.
DThe query counts NULLs in sold_count because NULL = 'sold' is true.
Attempts:
2 left
💡 Hint
Remember how NULL behaves in SQL comparisons.