Bird
0
0

What is wrong with this query?

medium📝 Debug Q7 of 15
PostgreSQL - Aggregate Functions and GROUP BY
What is wrong with this query?
SELECT department,
       SUM(amount) FILTER (WHERE status = 'approved') AS approved_sum,
       SUM(amount) FILTER (WHERE status = 'rejected') AS rejected_sum
FROM transactions;
AMissing GROUP BY clause for department
BFILTER clause cannot be used with SUM
CIncorrect use of WHERE inside FILTER
DNo error, query is valid
Step-by-Step Solution
Solution:
  1. Step 1: Check GROUP BY usage

    The query selects department and aggregates but does not group by department, which is required.
  2. Step 2: Validate FILTER and SUM usage

    FILTER with SUM and WHERE is correct syntax, so no error there.
  3. Final Answer:

    Missing GROUP BY clause for department -> Option A
  4. Quick Check:

    Grouping columns must appear in GROUP BY [OK]
Quick Trick: Use GROUP BY for all non-aggregated selected columns [OK]
Common Mistakes:
  • Forgetting GROUP BY when selecting non-aggregated columns
  • Thinking FILTER disallows SUM
  • Misusing WHERE inside FILTER

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes