-1

in postgreSQL database I have table with Name(string) column.

ID| Name
1 | a
2 | b
3 | c
4 | d

With one query I would like to retrieve all names from the table into one string in the format:

'a', 'b', 'c', 'd'
panmigal
  • 101
  • 2
  • 7

1 Answers1

1

You can use string_agg():

select string_agg(distinct '''' || name || '''', ', ' order by id)
from t;

For most purposes, though, I would suggest using arrays instead:

select array_agg(name order by id)
from t
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786