Updated May 12, 2023
Introduction to Oracle GROUP_CONCAT
GROUP_CONCAT function can be defined as an aggregate (GROUP BY) function in oracle which concatenates (it means to link together) all strings in a group, in a given order, separating them with a given separator and it returns a result of a string data type with the concatenated NOT NULL values from a group and in case there are no NOT NULL values in a group, the function returns the NULL value in that particular case.
Now, let us take a look at the Syntax of this function.
Syntax:
GROUP_CONCAT ([DISTINCT] expression [, expression ...]
[ORDER BY {col_name}
[ASC | DESC] [, col_name ...]]
[SEPARATOR str_val])
Parameters
- Distinct: It eliminates the repetition of values from the result set.
- ORDER BY: It is used to sort the elements in ascending or descending order.
- expression: It refers to the expression.
- col_name: It refers to the name of the column.
- Separator: The separator refers to the character which will separate the values of the group. The default separator is the comma (,).
How Does the Function Group_Concat Work?
In the earlier section of this article, we discussed the definition of the group_concat function. Let us now discuss how the function work does. The group_concat function is an aggregate function that concatenates multiple data from multiple rows into a single file. Suppose we want to find the data of all employees’ first names from a particular table employee based on the city of the employee. This function makes it easy. So when we execute this function, the function will group all data set based on the unique cities by using the group by clause. Now the function will concatenate all the names present in each group into a single row with each element present in the single row separated by a comma (default). If we provide DISTINCT function then it will only allow the unique values in the single field.
Let us now look at various scenarios along with some examples.
Examples of Group_Concat Function
It is very important to go through the examples to understand better the different scenarios in which we can use group_concat function.
Example #1 – Using Basic Group_Concat Function
In this scenario, we will use the function to find the list of names of employees belonging to a particular city present in the employee’s table. One point to note is that even if the city is null or not present it will be considered as a group. Let us now look at the query for the same.
Query:
SELECT city, GROUP_CONCAT (name) as "NAMES"
FROM employee group by city;
In this query as we can see that we have not provided any separator but the database will by default take comma as the separator in case nothing explicitly has been mentioned by the developer.
Let us now execute the query and look at the result.
As we can see in the above screenshot the names of employees from every city have been shown in the output.
Example #2 – Using the Group_Concat Distinct
In this scenario, we will use DISTINCT with the group_concat function. The reason for using a DISTINCT keyword is that it will show only the unique values belonging to every group. No duplicate values will be allowed. In this example, we will print only the unique employee First names belonging to every city in the employee table. Here the table will be grouped by the city so that each city becomes a group. Let us look at the query for the same.
Query:
SELECT city, GROUP_CONCAT (DISTINCT name) as "UNIQUE NAMES"
FROM employee group by city;
As we can see that in the query we have just added the DISTINCT keyword with the name column. So there is not much of a change in the query from the previous example.
Let us execute the query and check the output result.
As we can see in the output screenshot, there are no duplicate names in each row.
Example #3 – Using a Different Separator with Group_Concat
In this scenario, we will use a different separator like ‘/’. As we know that the default separator is the comma (,). We will use the same example as above. The only difference is that we will use a different separator ‘/’. Let us look at the query for the same.
Query:
SELECT city, GROUP_CONCAT (DISTINCT name SEPARATOR ‘/’) as "UNIQUE NAMES"
FROM employee group by city;
As we can see that in the query we have added a different separator ‘/‘. This will result in ‘/’ being used as a separator between the employee names.
Let us run the query in SQL developer and check the result.
In the above screenshot, we can see that each employee name in each row of the unique names column is separated by the character ‘/’.
Example #4 – Using Order by Clause with Group_Concat
In this scenario, we will use the ORDER BY clause with the GROUP_CONCAT function. The ORDER BY clause sorts the values of the group in a specific order and after that, it concatenates every sorted value of the group. In this example, we will print the employee names of each distinct employee of a particular city in descending order. The query will first group the table based on the city and then the distinct names belonging to every group will be sorted in descending order and then all the sorted values will be concatenated. Let us look at the query for the same.
Query:
SELECT city, GROUP_CONCAT (DISTINCT name ORDER BY name DESC SEPARATOR ‘/’) as "UNIQUE NAMES"
FROM employee group by city;
Let us execute the query and check the result.
As we can see in the above screenshot, the values are sorted in descending order.
Conclusion – Oracle GROUP_CONCAT
In this article, we discussed the definition and syntax of the GROUP_CONCAT function in the beginning. Later on, in the article, we discussed the working of the GROUP_CONCAT function along with different scenarios and examples to have a better understanding of the function.
Recommended Articles
This is a guide to Oracle GROUP_CONCAT. Here we discuss How Does the Function Group_Concat Work along with the examples. You may also look at the following articles to learn more –