Bird
0
0

Which of the following is the correct way to sum the price column only for rows where status = 'shipped' using the FILTER clause in PostgreSQL?

easy📝 Syntax Q3 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Which of the following is the correct way to sum the price column only for rows where status = 'shipped' using the FILTER clause in PostgreSQL?
ASELECT SUM(price) FILTER (WHERE status = 'shipped') FROM orders;
BSELECT SUM(price) WHERE status = 'shipped' FILTER FROM orders;
CSELECT SUM(price) FROM orders FILTER (status = 'shipped');
DSELECT SUM(price) FILTER status = 'shipped' FROM orders;
Step-by-Step Solution
Solution:
  1. Step 1: Recall FILTER syntax

    The correct syntax is AGG_FUNC(...) FILTER (WHERE condition).
  2. Step 2: Analyze options

    SELECT SUM(price) FILTER (WHERE status = 'shipped') FROM orders; matches the correct syntax. Options B, C, and D misuse the FILTER clause placement or syntax.
  3. Final Answer:

    SELECT SUM(price) FILTER (WHERE status = 'shipped') FROM orders; -> Option A
  4. Quick Check:

    FILTER clause must follow aggregate with WHERE inside parentheses [OK]
Quick Trick: FILTER clause syntax: AGG_FUNC(...) FILTER (WHERE condition) [OK]
Common Mistakes:
  • Placing FILTER after FROM clause
  • Omitting WHERE keyword inside FILTER
  • Using FILTER without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes