{"id":21523,"date":"2021-11-29T07:41:59","date_gmt":"2021-11-28T21:41:59","guid":{"rendered":"https:\/\/database.guide\/?p=21523"},"modified":"2021-11-29T07:41:59","modified_gmt":"2021-11-28T21:41:59","slug":"postgresql-group_concat-equivalent","status":"publish","type":"post","link":"https:\/\/database.guide\/postgresql-group_concat-equivalent\/","title":{"rendered":"PostgreSQL GROUP_CONCAT() Equivalent"},"content":{"rendered":"\n<p>Some RDBMSs like MySQL and MariaDB have a <code>GROUP_CONCAT()<\/code> function that allows you to return a query column as a delimited list (for example, a comma separated list).<\/p>\n\n\n\n<p>PostgreSQL has a similar function called <code><a href=\"https:\/\/database.guide\/string_agg-function-in-postgresql\/\" data-type=\"post\" data-id=\"21509\">STRING_AGG()<\/a><\/code>. This function works in pretty much the same way that <code>GROUP_CONCAT()<\/code> works in MySQL and MariaDB.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s a basic example to demonstrate how PostgreSQL&#8217;s <code>STRING_AGG()<\/code> function works:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT STRING_AGG(genre, ',') FROM Genres;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Rock,Jazz,Country,Pop,Blues,Hip Hop,Rap,Punk<\/pre>\n\n\n\n<p>Here&#8217;s what happens when we just do the query without the <code>STRING_AGG()<\/code> function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT genre FROM Genres;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------+\n|  genre  |\n+---------+\n| Rock    |\n| Jazz    |\n| Country |\n| Pop     |\n| Blues   |\n| Hip Hop |\n| Rap     |\n| Punk    |\n+---------+\n(8 rows)<\/pre>\n\n\n\n<p>We get eight rows, each with a separate value, instead of one long comma separated row of all values.<\/p>\n\n\n\n<p>If anything, PostgreSQL&#8217;s <code>STRING_AGG()<\/code> is more like MySQL&#8217;s <code>GROUP_CONCAT()<\/code> than MariaDB&#8217;s function of the same name. I say this because MariaDB&#8217;s <code>GROUP_CONCAT()<\/code> allows us to provide a <code>LIMIT<\/code> clause (as from MariaDB 10.3.3), right from within the function itself. MySQL <code>GROUP_CONCAT()<\/code> doesn&#8217;t support the <code>LIMIT<\/code> clause, and neither does PostgreSQL&#8217;s <code>STRING_AGG()<\/code> function (at least, not at the time of this writing).<\/p>\n\n\n\n<p>Another difference is that PostgreSQL&#8217;s <code>STRING_AGG()<\/code> requires a second argument (that specifies the delimiter to use). Both MySQL and MariaDB make this optional with their <code>GROUP_CONCAT()<\/code> functions.<\/p>\n\n\n\n<p>Postgres&#8217;s <code>STRING_AGG()<\/code> accepts an <code>ORDER BY<\/code> clause, and a <code>DISTINCT<\/code> clause (as do MariaDB and MySQL&#8217;s <code>GROUP_CONCAT()<\/code> functions).<\/p>\n\n\n\n<p>See <a href=\"https:\/\/database.guide\/string_agg-function-in-postgresql\/\" data-type=\"post\" data-id=\"21509\"><code>STRING_AGG()<\/code> Function in PostgreSQL<\/a> for more examples.<\/p>\n\n\n\n<p>And in case you&#8217;re interested, <a href=\"https:\/\/database.guide\/how-to-return-query-results-as-a-comma-separated-list-in-sql-server\/\" data-type=\"post\" data-id=\"2221\">SQL Server also has a <code>STRING_AGG()<\/code> function<\/a> that works in pretty much the same way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some RDBMSs like MySQL and MariaDB have a GROUP_CONCAT() function that allows you to return a query column as a delimited list (for example, a comma separated list). PostgreSQL has a similar function called STRING_AGG(). This function works in pretty much the same way that GROUP_CONCAT() works in MySQL and MariaDB.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[73,113,93,10],"class_list":["post-21523","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-aggregate-functions","tag-aggregation","tag-functions","tag-how-to"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=21523"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21523\/revisions"}],"predecessor-version":[{"id":21537,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21523\/revisions\/21537"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=21523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=21523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=21523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}