{"id":2530,"date":"2013-05-19T17:59:01","date_gmt":"2013-05-20T01:59:01","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=2530"},"modified":"2023-11-09T17:59:45","modified_gmt":"2023-11-10T00:59:45","slug":"mysql-group_concat-function","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/","title":{"rendered":"MySQL GROUP_CONCAT() Function"},"content":{"rendered":"\n<p><b>Summary<\/b>: in this tutorial, you will learn how to use the MySQL <code>GROUP_CONCAT()<\/code>&nbsp;function to concatenate strings from a group with various options.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL GROUP_CONCAT() function<\/h2>\n\n\n\n<p>The MySQL <code>GROUP_CONCAT()<\/code> function is an <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/\">aggregate function<\/a> that concatenates strings from a group into a single string with various options.<\/p>\n\n\n\n<p>The following shows the syntax of the <code>GROUP_CONCAT()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">GROUP_CONCAT(\n    DISTINCT expression\n    ORDER BY expression\n    SEPARATOR sep\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example demonstrates how the <code>GROUP_CONCAT()<\/code> function works.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> t (\n    v <span class=\"hljs-built_in\">CHAR<\/span>\n);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> t(v) <span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'A'<\/span>),(<span class=\"hljs-string\">'B'<\/span>),(<span class=\"hljs-string\">'C'<\/span>),(<span class=\"hljs-string\">'B'<\/span>);\n\n<span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(<span class=\"hljs-keyword\">DISTINCT<\/span> v\n        <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> v <span class=\"hljs-keyword\">ASC<\/span>\n        SEPARATOR <span class=\"hljs-string\">';'<\/span>)\n<span class=\"hljs-keyword\">FROM<\/span>\n    t;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"84\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-function-quick-example.jpg\" alt=\"MySQL GROUP_CONCAT function quick example\" class=\"wp-image-4591\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"629\" height=\"210\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MSQL-group_concat.png\" alt=\"MSQL group_concat\" class=\"wp-image-4592\" title=\"MSQL group_concat\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MSQL-group_concat.png 629w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MSQL-group_concat-300x100.png 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/figure>\n\n\n\n<p>The <code>DISTINCT<\/code>&nbsp;clause allows you to eliminate duplicate values in the group before concatenating them.<\/p>\n\n\n\n<p>The <code>ORDER BY<\/code>&nbsp;clause allows you to sort the values in ascending or descending order before concatenating. By default, it sorts the values in ascending order. If you want to sort the values in the descending order, you need to specify explicitly the <code>DESC<\/code> option.<\/p>\n\n\n\n<p>The <code>SEPARATOR<\/code> specifies a literal value inserted between values in the group. If you do not specify a separator, the <code>GROUP_CONCAT<\/code> function uses a comma (<code>,<\/code>) as the default separator.<\/p>\n\n\n\n<p>The <code>GROUP_CONCAT<\/code> function ignores <code>NULL<\/code> values. It returns <code>NULL<\/code> if there are no matching rows or all arguments are <code>NULL<\/code> values.<\/p>\n\n\n\n<p>The <code>GROUP_CONCAT<\/code> function returns a binary or non-binary string, which depends on the arguments. <\/p>\n\n\n\n<p>By default, the maximum length of the return string is 1024. If you need more than this, you can extend the maximum length by setting the <code>group_concat_max_len<\/code> system variable at <code>SESSION<\/code> or <code>GLOBAL<\/code> level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL GROUP_CONCAT() function examples<\/h2>\n\n\n\n<p>Let&#8217;s take a look at the <code>customers<\/code> table in the <a title=\"MySQL Sample Database\" href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/customers.svg\" alt=\"\" class=\"wp-image-10765\"\/><\/figure>\n\n\n\n<p>To get all countries where customers are located as a comma-separated string, you use the <code>GROUP_CONCAT()<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(country)\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-group_concat\/#1\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"299\" height=\"45\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-Function-example.jpg\" alt=\"MySQL GROUP_CONCAT Function example\" class=\"wp-image-4584\"\/><\/figure>\n\n\n\n<p>However, some customers are located in the same country. To remove the duplicate country&#8217;s names, you add the <code>DISTINCT<\/code> clause as the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(<span class=\"hljs-keyword\">DISTINCT<\/span> country)\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-group_concat\/#2\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"352\" height=\"57\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-DISTINCT-example.jpg\" alt=\"MySQL GROUP_CONCAT DISTINCT example\" class=\"wp-image-4585\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-DISTINCT-example.jpg 352w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-DISTINCT-example-300x49.jpg 300w\" sizes=\"auto, (max-width: 352px) 100vw, 352px\" \/><\/figure>\n\n\n\n<p>It is more readable if the country&#8217;s names are in ascending order. To sort the country&#8217;s name before concatenating, you use the <code>ORDER BY<\/code> clause as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(<span class=\"hljs-keyword\">DISTINCT<\/span> country\n        <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> country)\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-group_concat\/#3\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"275\" height=\"69\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-with-ORDER-BY-example.jpg\" alt=\"MySQL GROUP_CONCAT with ORDER BY example\" class=\"wp-image-4587\"\/><\/figure>\n\n\n\n<p>To change the default separator of the returned string from a comma (,) to a semi-colon (;), you use the <code>SEPARATOR<\/code> clause as the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(<span class=\"hljs-keyword\">DISTINCT<\/span> country\n        <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> country\n        SEPARATOR <span class=\"hljs-string\">';'<\/span>)\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-group_concat\/#4\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"276\" height=\"85\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-with-SEPARATOR-example.jpg\" alt=\"MySQL GROUP_CONCAT with SEPARATOR example\" class=\"wp-image-4589\" title=\"MySQL GROUP_CONCAT with SEPARATOR example\"\/><\/figure>\n\n\n\n<p>Great! now you know how the <code>GROUP_CONCAT()<\/code> function works. Let&#8217;s put it in a practical example.<\/p>\n\n\n\n<p>Each customer has one or more sales representatives. In other words, each sales employee is in charge of one or more customers. To find out who is in charge of which customers, you use the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-inner-join\/\">inner join<\/a> clause as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    employeeNumber, \n    firstname, \n    lastname, \n    customername\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n    customers <span class=\"hljs-keyword\">ON<\/span> customers.salesRepEmployeeNumber = employees.employeeNumber\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n\tfirstname, \n    lastname;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-group_concat\/#5\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"401\" height=\"291\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/GROUP_CONCAT-with-INNER-JOIN-and-GROUP-BY.jpg\" alt=\"GROUP_CONCAT with INNER JOIN and GROUP BY\" class=\"wp-image-5035\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/GROUP_CONCAT-with-INNER-JOIN-and-GROUP-BY.jpg 401w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/GROUP_CONCAT-with-INNER-JOIN-and-GROUP-BY-300x218.jpg 300w\" sizes=\"auto, (max-width: 401px) 100vw, 401px\" \/><\/figure>\n\n\n\n<p>Now, we can group the result set by the employee number and concatenate all employees that are in charge of the employee by using the <code>GROUP_CONCAT()<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    employeeNumber,\n    firstName,\n    lastName,\n    <span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(<span class=\"hljs-keyword\">DISTINCT<\/span> customername\n        <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> customerName)\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees\n        <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n    customers <span class=\"hljs-keyword\">ON<\/span> customers.salesRepEmployeeNumber = employeeNumber\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> employeeNumber\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> firstName , lastname;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-group_concat\/#6\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"949\" height=\"295\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/GROUP_CONCAT-with-INNER-JOIN-and-GROUP-BY-Example.jpg\" alt=\"GROUP_CONCAT with INNER JOIN and GROUP BY Example\" class=\"wp-image-5036\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/GROUP_CONCAT-with-INNER-JOIN-and-GROUP-BY-Example.jpg 949w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/GROUP_CONCAT-with-INNER-JOIN-and-GROUP-BY-Example-300x93.jpg 300w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/GROUP_CONCAT-with-INNER-JOIN-and-GROUP-BY-Example-768x239.jpg 768w\" sizes=\"auto, (max-width: 949px) 100vw, 949px\" \/><\/figure>\n\n\n\n<p>The result set is much easier to read.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using GROUP_CONCAT() with CONCAT_WS() function example<\/h3>\n\n\n\n<p>Sometimes, the <code>GROUP_CONCAT<\/code> function can be combined with the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-string-functions\/mysql-concat_ws\/\">CONCAT_WS<\/a> function to make the result of the query more useful.<\/p>\n\n\n\n<p>For example, to make a list of semicolon-separated values of customers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, you concatenate the last name and first name of each customer&#8217;s contact using the <code>CONCAT_WS()<\/code> function. The result is the contact&#8217;s full name.<\/li>\n\n\n\n<li>Then, you use the <code>GROUP_CONCAT()<\/code> function to make the list.<\/li>\n<\/ul>\n\n\n\n<p>The following query makes a list of semicolon-separated values of customers.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(\n       <span class=\"hljs-keyword\">CONCAT_WS<\/span>(<span class=\"hljs-string\">', '<\/span>, contactLastName, contactFirstName)\n       SEPARATOR <span class=\"hljs-string\">';'<\/span>)\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-group_concat\/#7\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<p>Note that <code>GROUP_CONCAT()<\/code> function concatenates string values in different rows while the <code>CONCAT_WS()<\/code> or <code>CONCAT()<\/code>function concatenates two or more string values in different columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">GROUP_CONCAT function: common mistakes<\/h2>\n\n\n\n<p>The <code>GROUP_CONCAT()<\/code> function returns a single string, not a list of values. It means you cannot use the result of the <code>GROUP_CONCAT()<\/code> function for <a title=\"MySQL IN\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-in\/\">IN <\/a>operator e.g., within a <a title=\"MySQL Subquery\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-subquery\/\">subquery<\/a>.<\/p>\n\n\n\n<p>For example, the <code>GROUP_CONCAT()<\/code> function returns the result of values:<code>1<\/code> <code>2<\/code>, and <code>3<\/code> as the &#8216;1,2,3&#8217; string.<\/p>\n\n\n\n<p>If you supply this result to the <code>IN<\/code> operator, the query is not working. Therefore, the query may not return any result. For example, the following query will not work as desired.<\/p>\n\n\n\n<p>Because the <code>IN<\/code> operator accepts a list of values e.g., (1,2,3), not a string that consists of a list of values (&#8216;1,2,3&#8217;). As a result, the following query will not work as expected.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">id<\/span>, <span class=\"hljs-keyword\">name<\/span>\n<span class=\"hljs-keyword\">FROM<\/span>\n    table_name\n<span class=\"hljs-keyword\">WHERE<\/span>\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-keyword\">IN<\/span> <span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(<span class=\"hljs-keyword\">id<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-group_concat\/#8\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<p>Because the <code>GROUP_CONCAT<\/code> function is an <a title=\"MySQL aggregate function\" href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/\">aggregate function<\/a>, to<span style=\"line-height: 1.5;\">&nbsp;sort the values, you must use the <\/span><code style=\"line-height: 1.5;\">ORDER BY<\/code><span style=\"line-height: 1.5;\"> clause inside the function, not in the <\/span><code style=\"line-height: 1.5;\">ORDER BY<\/code> <span style=\"line-height: 1.5;\"> in the <\/span><a style=\"line-height: 1.5;\" title=\"MySQL SELECT statement\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">SELECT <\/a><span style=\"line-height: 1.5;\">statement.<\/span><\/p>\n\n\n\n<p><span style=\"line-height: 1.5;\">The following example demonstrates the incorrect usage of the <\/span><code style=\"line-height: 1.5;\">ORDER BY<\/code><span style=\"line-height: 1.5;\"> clause in the context of using the <\/span><code style=\"line-height: 1.5;\">GROUP_CONCAT<\/code><span style=\"line-height: 1.5;\"> function:<\/span><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">GROUP_CONCAT<\/span>(<span class=\"hljs-keyword\">DISTINCT<\/span> country\n        SEPARATOR <span class=\"hljs-string\">';'<\/span>)\n<span class=\"hljs-keyword\">FROM<\/span>\n    customers\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> country;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.mysqltutorial.org\/tryit\/query\/mysql-group_concat\/#9\" target=\"_blank\" rel=\"noopener noreferrer\">Try It Out<\/a><\/p>\n\n\n\n<p>The <code>SELECT<\/code> clause returns one string value so the <code>ORDER BY<\/code> clause does not take any effect in this statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL GROUP_CONCAT() function applications<\/h2>\n\n\n\n<p>There are many cases where you can apply the <code>GROUP_CONCAT()<\/code> function to produce useful results. The following list is some common examples of using the <code>GROUP_CONCAT()<\/code> function.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Make a comma-separated user&#8217;s roles such as &#8216;admin, author, editor&#8217;.<\/li>\n\n\n\n<li>Produce the comma-separated user&#8217;s hobbies e.g., &#8216;design, programming, reading&#8217;.<\/li>\n\n\n\n<li>Create tags for blog posts, articles, or products e.g., &#8216;mysql, mysql aggregate function, mysql tutorial&#8217;.<\/li>\n<\/ul>\n\n\n\n<p>In this tutorial, you have learned how to use the MySQL <code>GROUP_CONCAT()<\/code> function to concatenate non-<code>NULL<\/code> values of a group of strings into a single string.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2530\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/\"\n\t\t\t\tdata-post-title=\"MySQL GROUP_CONCAT() Function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2530\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/\"\n\t\t\t\tdata-post-title=\"MySQL GROUP_CONCAT() Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the MySQL GROUP_CONCAT function to concatenate strings from a group with various options.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":509,"menu_order":9,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2530","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL GROUP_CONCAT Function<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the MySQL GROUP_CONCAT function to concatenate strings from a group with various options.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL GROUP_CONCAT Function\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the MySQL GROUP_CONCAT function to concatenate strings from a group with various options.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T00:59:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-function-quick-example.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-group_concat-function\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-group_concat-function\\\/\",\"name\":\"MySQL GROUP_CONCAT Function\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-group_concat-function\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-group_concat-function\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/05\\\/MySQL-GROUP_CONCAT-function-quick-example.jpg\",\"datePublished\":\"2013-05-20T01:59:01+00:00\",\"dateModified\":\"2023-11-10T00:59:45+00:00\",\"description\":\"This tutorial shows you how to use the MySQL GROUP_CONCAT function to concatenate strings from a group with various options.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-group_concat-function\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-group_concat-function\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-group_concat-function\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/05\\\/MySQL-GROUP_CONCAT-function-quick-example.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/05\\\/MySQL-GROUP_CONCAT-function-quick-example.jpg\",\"width\":188,\"height\":84},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/mysql-group_concat-function\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Aggregate Functions\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-aggregate-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL GROUP_CONCAT() Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL GROUP_CONCAT Function","description":"This tutorial shows you how to use the MySQL GROUP_CONCAT function to concatenate strings from a group with various options.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/","og_locale":"en_US","og_type":"article","og_title":"MySQL GROUP_CONCAT Function","og_description":"This tutorial shows you how to use the MySQL GROUP_CONCAT function to concatenate strings from a group with various options.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-11-10T00:59:45+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-function-quick-example.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/","url":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/","name":"MySQL GROUP_CONCAT Function","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-function-quick-example.jpg","datePublished":"2013-05-20T01:59:01+00:00","dateModified":"2023-11-10T00:59:45+00:00","description":"This tutorial shows you how to use the MySQL GROUP_CONCAT function to concatenate strings from a group with various options.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-function-quick-example.jpg","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/05\/MySQL-GROUP_CONCAT-function-quick-example.jpg","width":188,"height":84},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-group_concat-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Aggregate Functions","item":"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/"},{"@type":"ListItem","position":3,"name":"MySQL GROUP_CONCAT() Function"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=2530"}],"version-history":[{"count":4,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2530\/revisions"}],"predecessor-version":[{"id":12242,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2530\/revisions\/12242"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/509"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=2530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}