You can concatenate strings separated by delimiter using the string_agg()
function.
If your individuals
table is:
Name | Age | Country |
---|---|---|
Allie | 15 | USA |
Amanda | 14 | USA |
Alana | 20 | Russia |
You could write SELECT ... GROUP BY
statement to get names from each country:
SELECT string_agg(name, ', ') AS names, country
FROM individuals
GROUP BY country;
Note that you need to use a GROUP BY
clause because string_agg()
is an aggregate function.
Result:
names | country |
---|---|
Allie, Amanda | USA |
Alana | Russia |
More PostgreSQL aggregate function described here