Bird
0
0

Why might the order of elements in the array returned by ARRAY_AGG differ between query executions if no ORDER BY is specified inside it?

hard📝 Conceptual Q10 of 15
PostgreSQL - Aggregate Functions and GROUP BY
Why might the order of elements in the array returned by ARRAY_AGG differ between query executions if no ORDER BY is specified inside it?
ABecause ARRAY_AGG always sorts elements alphabetically by default
BBecause SQL tables represent unordered sets, so aggregation order is not guaranteed
CBecause ARRAY_AGG caches results and returns them in fixed order
DBecause GROUP BY forces sorting of aggregated arrays
Step-by-Step Solution
Solution:
  1. Step 1: Understand SQL table ordering

    Tables represent unordered sets; row order is not guaranteed without ORDER BY.
  2. Step 2: Effect on ARRAY_AGG

    Without ORDER BY inside ARRAY_AGG, element order depends on query plan and can vary.
  3. Final Answer:

    Because SQL tables represent unordered sets, so aggregation order is not guaranteed -> Option B
  4. Quick Check:

    ARRAY_AGG order is undefined without ORDER BY [OK]
Quick Trick: Always use ORDER BY inside ARRAY_AGG for consistent element order [OK]
Common Mistakes:
  • Assuming ARRAY_AGG sorts by default
  • Thinking GROUP BY sorts aggregated arrays
  • Believing ARRAY_AGG caches fixed order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes