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 STRING_AGG(name, ', ' ORDER BY name) FROM users ORDER BY name;
AORDER BY cannot be used inside STRING_AGG.
BThe query is correct and will run without errors.
CORDER BY must be inside STRING_AGG parentheses, not outside.
DORDER BY inside STRING_AGG must come after separator argument.
Step-by-Step Solution
Solution:
  1. Step 1: Check ORDER BY usage inside STRING_AGG

    ORDER BY inside STRING_AGG after the separator is valid syntax.
  2. Step 2: Check outer ORDER BY

    Without GROUP BY, the outer ORDER BY name causes an error because 'name' must appear in GROUP BY or be used in an aggregate function.
  3. Final Answer:

    ORDER BY must be inside STRING_AGG parentheses, not outside. -> Option C
  4. Quick Check:

    Outer ORDER BY invalid without GROUP BY = ORDER BY must be inside STRING_AGG parentheses, not outside. [OK]
Quick Trick: ORDER BY inside STRING_AGG works; outside requires GROUP BY in aggregate queries [OK]
Common Mistakes:
  • Thinking ORDER BY inside STRING_AGG is invalid
  • Confusing outer ORDER BY with inner
  • Misplacing ORDER BY arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes