Bird
0
0

Find the error in this query:

medium📝 Debug Q6 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Find the error in this query:
SELECT product_type,
       COUNT(*) FILTER (status = 'available') AS available_count
FROM inventory
GROUP BY product_type;
ACOUNT(*) cannot be used with FILTER clause.
BThe FILTER clause is missing the WHERE keyword inside parentheses.
CGROUP BY should include status column as well.
DThe alias 'available_count' is not allowed in this context.
Step-by-Step Solution
Solution:
  1. Step 1: Check FILTER syntax

    The FILTER clause requires the condition to be inside parentheses with a WHERE keyword: FILTER (WHERE condition).
  2. Step 2: Analyze the query

    The query uses FILTER (status = 'available') missing WHERE, which is a syntax error.
  3. Step 3: Verify other options

    COUNT(*) can be used with FILTER, GROUP BY does not need status here, and aliasing is allowed.
  4. Final Answer:

    The FILTER clause is missing the WHERE keyword inside parentheses. -> Option B
  5. Quick Check:

    FILTER requires WHERE keyword inside parentheses [OK]
Quick Trick: FILTER clause must include WHERE keyword inside parentheses [OK]
Common Mistakes:
  • Omitting WHERE inside FILTER clause
  • Assuming COUNT(*) cannot be filtered
  • Adding unnecessary columns to GROUP BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes