Bird
0
0

Identify the error in this query:

medium📝 Debug Q14 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Identify the error in this query:
SELECT product_id, region, COUNT(*) FROM sales GROUP BY product_id;
ACOUNT(*) cannot be used with GROUP BY.
BMissing region in GROUP BY causes error.
Cproduct_id cannot be grouped alone.
DSyntax error due to missing FROM clause.
Step-by-Step Solution
Solution:
  1. Step 1: Check SELECT columns vs GROUP BY columns

    SELECT includes product_id and region, but GROUP BY only has product_id.
  2. Step 2: Understand PostgreSQL grouping rules

    All non-aggregated columns in SELECT must appear in GROUP BY; region is missing.
  3. Final Answer:

    Missing region in GROUP BY causes error. -> Option B
  4. Quick Check:

    All SELECT non-aggregates must be in GROUP BY [OK]
Quick Trick: Include all non-aggregated SELECT columns in GROUP BY [OK]
Common Mistakes:
  • Forgetting to add all columns to GROUP BY
  • Thinking COUNT(*) disallows grouping
  • Ignoring SELECT and GROUP BY mismatch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes