Bird
0
0

Given the table orders with columns customer_id, status, and amount, what will the following query return?

medium📝 query result Q4 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Given the table orders with columns customer_id, status, and amount, what will the following query return?
SELECT customer_id,
       SUM(amount) FILTER (WHERE status = 'shipped') AS shipped_total,
       SUM(amount) FILTER (WHERE status = 'pending') AS pending_total
FROM orders
GROUP BY customer_id;
ACount of shipped and pending orders per customer
BTotal sum of all amounts ignoring status
CSum of amounts per customer for shipped and pending orders separately
DSum of amounts only for customers with shipped orders
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the query aggregates

    The query sums amounts grouped by customer_id, but uses FILTER to sum only amounts where status is 'shipped' or 'pending' separately.
  2. Step 2: Understand the output columns

    shipped_total sums amounts for shipped orders, pending_total sums amounts for pending orders, both per customer.
  3. Final Answer:

    Sum of amounts per customer for shipped and pending orders separately -> Option C
  4. Quick Check:

    FILTER sums conditionally per group = separate sums [OK]
Quick Trick: FILTER sums only rows matching condition inside group [OK]
Common Mistakes:
  • Thinking it counts rows instead of sums
  • Ignoring FILTER and summing all rows
  • Assuming only shipped orders are included

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes