I have a table in postgresql. The following table \"animals\" will do to explain my problem:
name
------
tiger
cat
dog
Now I am using the follo
For modern PostgreSQL (since version 9.0), you can use an ORDER BY clause in an aggregate expression:
SELECT
array_to_string(array_agg(name ORDER BY name), ', ')
FROM
animals;
Also, for your specific purpose, you can use string_agg to simplify your query:
SELECT
string_agg(name, ', ' ORDER BY name)
FROM
animals;