Bird
0
0

Find the error in this PostgreSQL query:

medium📝 Debug Q6 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Find the error in this PostgreSQL query:
SELECT product_type, COUNT(*) FROM inventory GROUP BY product_type HAVING COUNT(*) < 10 ORDER BY product_type;
AHAVING clause should come before GROUP BY.
BNo error; the query is correct.
CORDER BY cannot be used with GROUP BY.
DCOUNT(*) cannot be used in HAVING clause.
Step-by-Step Solution
Solution:
  1. Step 1: Check clause order

    Correct order is SELECT, FROM, GROUP BY, HAVING, ORDER BY.
  2. Step 2: Validate HAVING usage

    HAVING filters groups using aggregate functions like COUNT(*).
  3. Step 3: Confirm ORDER BY usage

    ORDER BY can be used after HAVING to sort results.
  4. Final Answer:

    No error; the query is correct. -> Option B
  5. Quick Check:

    HAVING filters groups; ORDER BY sorts results [OK]
Quick Trick: HAVING after GROUP BY, ORDER BY last [OK]
Common Mistakes:
  • Placing HAVING before GROUP BY
  • Assuming ORDER BY is invalid with GROUP BY
  • Misusing aggregate functions in WHERE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes