Bird
0
0

Which of the following is the correct syntax to count only rows where status = 'active' using FILTER in PostgreSQL?

easy📝 Syntax Q12 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Which of the following is the correct syntax to count only rows where status = 'active' using FILTER in PostgreSQL?
ASELECT COUNT(*) FILTER (WHERE status = 'active') FROM users;
BSELECT COUNT(*) FROM users FILTER (WHERE status = 'active');
CSELECT COUNT(*) WHERE FILTER status = 'active' FROM users;
DSELECT COUNT(*) FILTER WHERE status = 'active' FROM users;
Step-by-Step Solution
Solution:
  1. 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).
  2. 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.
  3. Final Answer:

    SELECT COUNT(*) FILTER (WHERE status = 'active') FROM users; -> Option A
  4. Quick Check:

    FILTER syntax = FILTER (WHERE condition) [OK]
Quick Trick: Use FILTER (WHERE condition) inside aggregate parentheses [OK]
Common Mistakes:
  • Omitting parentheses after FILTER
  • Placing FILTER outside aggregate function
  • Using FILTER without WHERE keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes