Ordering of Aggregate Functions in PostgreSQL

June 23, 2021 • 1 minute read

When using multiple aggregate functions in PostgreSQL, the set of results is iterated through once.

This is interesting behavior because it means that the order of the aggregated results is preserved across selection clauses. Looking at the output of the script below, we know that the three individuals are "George Martins", "Mary Wong" and "Amy Kelly".

    ARRAY_AGG(first_name) as 'FirstNames',
    ARRAY_AGG(last_name) as 'LastNames'

-- output
-- FirstNames | LastNames
-- { 'George', 'Mary', 'Amy' }, { 'Martins', 'Wong', 'Kelly' }


Happy Coding 🎉

All Blogs

Let's have a chat. Come find me on the web.

Fergus Farrell | 2022