binder-test=# select * from testapp_zoo; id | name | ------+------- 5589 | test | (1 row) binder-test=# select * from testapp_animal order by name; id | name | zoo_id | -------+------+--------- 12066 | a | 5589 | 12065 | b | 5589 | (2 rows) binder-test=# SELECT "testapp_zoo"."id", ARRAY_AGG("testapp_animal"."id" ORDER BY "testapp_zoo"."name") FILTER (WHERE "testapp_animal"."id" IS NOT NULL) AS "animals___annotation" FROM "testapp_zoo" LEFT OUTER JOIN "testapp_animal" ON ("testapp_zoo"."id" = "testapp_animal"."zoo_id") GROUP BY "testapp_zoo"."id" ORDER BY "testapp_zoo"."name" ASC; id | animals___annotation ------+---------------------- 5589 | {12065,12066} (1 row)