EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle GROUP_CONCAT

Oracle GROUP_CONCAT

Oracle GROUP_CONCAT

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

Oracle GROUP_CONCAT output 1

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.

Oracle GROUP_CONCAT output 2

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.

output 3

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.

output 4

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 –

  1. Oracle COMMIT 
  2. Oracle WILDCARDS
  3. Function in Oracle
  4. OUTER Join in Oracle
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Popular Course in this category
ORACLE DBA Database Management System Course
 28+ Hours of HD Videos
2 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more