string_agg()

Concatenates the non-null input values into a string. Each value after the first is preceded by the corresponding delimiter (if it's not null).

Signature

string_agg ( value text, delimiter text ) → text
string_agg ( value bytea, delimiter bytea ORDER BY input_sort_columns ) → bytea

Examples

SELECT string_agg(product, ', ' ORDER BY product) FROM orders;
                     string_agg                     
----------------------------------------------------
 cable, keyboard, keyboard, monitor, monitor, mouse
(1 row)

Run against PostgreSQL 18.6 on 2026-09-13. This is the output we got, not the documentation’s.

In other engines

PostgreSQLstring_agg
MySQLGROUP_CONCAT(x SEPARATOR ', ')
SQL ServerSTRING_AGG(x, ', ')
OracleLISTAGG(x, ', ') WITHIN GROUP (ORDER BY x)
SQLitegroup_concat(x, ', ')

MySQL silently truncates at group_concat_max_len, 1024 bytes by default — the query succeeds and the answer is short. Oracle requires the WITHIN GROUP ordering clause; PostgreSQL and SQL Server take ORDER BY inside the call and are happy without it.

Want to run this against your own PostgreSQL server from an iPhone, iPad or Mac? These are ours — every connection goes straight from the device to your database, with no account and no server of ours in between.

Signature and description adapted from the PostgreSQL manual, used under the PostgreSQL License. PostgreSQL is a registered trademark of the PostgreSQL Community Association of Canada. This page is not affiliated with the PostgreSQL Global Development Group.