EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Oracle GROUP_CONCAT

Home » Data Science » Data Science Tutorials » Oracle Tutorial » 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;

Popular Course in this category
Sale
Oracle Training (14 Courses, 8+ Projects)14 Online Courses | 8 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (9,298 ratings)
Course Price

View Course

Related Courses
Oracle DBA Database Management System Training (2 Courses)All in One Financial Analyst Bundle - 250+ Courses, 40+ Projects

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 Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

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

© 2022 - 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

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

EDUCBA Login

Forgot Password?

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

Let’s Get Started

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Oracle Course Training Learn More