Concatenating column values in PostgreSQL with string_agg

PostgreSQL is an amazing database, no surprises here. But every now and then I discover an amazing little feature that I was missing, this week was the time to discover string_agg.

Suppose that you have two tables course and student and want to list every student enrolled in a given discipline.

Usually I did this with a quick Python script by grouping the course and than using than making a simple ', '.join(). With the aggregate string_agg function I now do it directly in the query.

Continue reading “Concatenating column values in PostgreSQL with string_agg”