groupConcat
Calculates a concatenated string from a group of strings, optionally separated by a delimiter, and optionally limited by a maximum number of elements.
Syntax
groupConcat[(delimiter [, limit])](expression);
Arguments
expression
— The expression or column name that outputs strings to be concatenated.delimiter
— A string that will be used to separate concatenated values. This parameter is optional and defaults to an empty string or delimiter from parameters if not specified.
Parameters
delimiter
— A string that will be used to separate concatenated values. This parameter is optional and defaults to an empty string if not specified.limit
— A positive integer specifying the maximum number of elements to concatenate. If more elements are present, excess elements are ignored. This parameter is optional.
If delimiter is specified without limit, it must be the first parameter. If both delimiter and limit are specified, delimiter must precede limit.
Also, if different delimiters are specified as parameters and arguments, the delimiter from arguments will be used only.
Returned value
- Returns a string consisting of the concatenated values of the column or expression. If the group has no elements or only null elements, and the function does not specify a handling for only null values, the result is a nullable string with a null value.
Examples
Input table:
┌─id─┬─name─┐
│ 1 │ John │
│ 2 │ Jane │
│ 3 │ Bob │
└────┴──────┘
- Basic usage without a delimiter:
Query:
SELECT groupConcat(Name) FROM Employees;
Result:
JohnJaneBob
This concatenates all names into one continuous string without any separator.
- Using comma as a delimiter:
Query:
SELECT groupConcat(', ')(Name) FROM Employees;
or
SELECT groupConcat(Name, ', ') FROM Employees;
Result:
John, Jane, Bob
This output shows the names separated by a comma followed by a space.
- Limiting the number of concatenated elements
Query:
SELECT groupConcat(', ', 2)(Name) FROM Employees;
Result:
John, Jane
This query limits the output to the first two names, even though there are more names in the table.