Oracle RDBMS 11gR2 – LISTAGG – New aggregation operator for creating (comma) delimited strings
In our series on the 11g R2 release of the Oracle RDBMS, AMIS staff discusses various functions, features an facilities that have been added, improved or extended in this latest release. These articles are the fruit of several months of studying this new release and projecting which new features and changes would be the most useful to us in our daily practice. This article introduced the LISTAGG operator, new in 11gR2.
An example usage of LISTAGG:
select deptno
, avg(sal) avg_sal
, listagg( ename, ',')
within group (order by sal)
enames
from emp
group
by deptno
Oracle RDBMS 10g introduced the Collect operator. It could be used to aggregate values on all rows in a group into a collection – a predefined TABLE OF <some type>. This operator can be used in statements like this one:
create or replace type
ename_type as table of varchar2(30)
/
select deptno
, avg(sal) avg_sal
, cast
( collect(ename)
as ename_type
) enames
from emp
group
by deptno
/

There are some limitations on using this operator: it does not support an order by especially for the collected values, it requires a TYPE definition (usually TABLE OF VARCHAR2) that has to be created before the Collect can be used and it acts only as a pure Aggregate Function (typically in conjunction with a group by), it is not available as an Analytical function.
(edited) on second thoughts, I should have rephrased the above. First of all – as Laurent pointed out in his comment, an order by expression apparently is allowed with COLLECT (I never knew that one!) just as DISTINCT and UNIQUE can be used. I also found out that you can use COLLECT without predefining a collection type. If you execute a statement like collect(ename) as emps – without the cast to turn the collect outcome into a well defined collection, the database will generate a type for you, on the fly with a system generated name that is quite worthless, but it will work all the same.
The new LISTAGG is similar to yet different from the COLLECT: LISTAGG creates a delimiter (comma, period, colon, etc.) separated string of values. The LISTAGG takes an optional order by clause that specifies the sequence of the values in the string. It returns a VARCHAR2 – so no custom type needs to be set up. And: LISTAGG is available as Aggregate (with GROUP BY) and Analytical Function (with PARTITION clause).
A simple example of using LISTAGG:
select deptno
, avg(sal) avg_sal
, listagg( ename, ',') within group (order by sal)
enames
from emp
group
by deptno
/
The results returned by this query:

Here we used LISTAGG as an aggregate function, together with the GROUP BY that in this case indicates that the concattenated string with ENAME values should create this string per department. Within the string, the enames should be ordered by the salary values of the employees.
The next example is on using the LISTAGG as an analytical function, producing aggregate values in line with the records rather than grouped by into a summary result. So for each record processed, the delimiter separated string is produced, in the same way other analytical functions function. That means for example that we could have a ‘running delimited string’ if we would add an order by to the over() clause without a windowing clause.
Here we are after the list of employees (colleagues) that work in the same department as the employee being processed:
select deptno
, ename
, sal
, listagg( ename, ',')
within group (order by ename)
over (partition by deptno)
colleagues
from emp
order
by deptno
, ename
The results:

Resources
11gR2 SQL Language Reference on Collect
11gR2 SQL Language Reference on LISTAGG
the collect function in 10g – on Oracle-developer.net
Oracle 11gR2 – alternative for CONNECT_BY_ISLEAF function for Recursive Subquery Factoring (dedicated to Anton)
14/11/2009 - 8:02 am
Tags: 11gr2, analytical function, connect by, hierarchy, isleaf, lead, recursive subquery, sql
Posted in Database, Devel. + PL/SQL tools, General, Oracle | 3 comments
On our blog, we have been discussing the new hierarchical query functionality in Oracle Database 11g Release 2, using Recursive Suquery Factoring. Instead of using CONNECT BY and its close associates such as START WITH, PRIOR, LEVEL and more exotic comrades like SYS_CONNECT_BY_PATH, CONNECT_BY_ROOT and NOCYCLE this release gave us a new, less proprietary and [...]
AMIS Query – Verslag van Oracle Open World 2009 – dinsdag 27 oktober (uitnodiging)
23/10/2009 - 7:12 am
Tags: 11gr2, Database, FMW 11g, fusion, hardware, Java, middleware, OOW 2009, Oracle, soa, sun
Posted in ADF & JHeadstart, Database, Devel. + PL/SQL tools, General, Java, JEE, OAS and WebLogic Server, Oracle, SOA & Oracle Fusion Middleware | 2 comments
Vorige week was in San Francisco het hoogtepunt van het Oracle jaar: Oracle Open World 2009, de grootste IT conferentie ter wereld. Tijdens deze conferentie ontvouwde Oracle haar strategie en visie voor de komende periode, lieten product managers de nabije toekomst zien van bestaande en nieuwe producten, deelden honderden specialisten hun ervaringen en toonden leveranciers [...]
SOA & SOA Suite for Oracle Database Professionals – seminars in Perth and Melbourne and Singapore (November 2009)
20/10/2009 - 10:18 pm
Tags: Database, plsql, seminar, soa, sql, university
Posted in Database, Devel. + PL/SQL tools, Oracle, SOA & Oracle Fusion Middleware | 3 comments
Next month, I will visit Australia and Singapore to present on SOA and the Oracle SOA Suite – to Oracle database developers. In this one-day-long seminar, I introduce the key concepts and objectives of SOA (Service Oriented Architecture) as well as the Oracle SOA Suite 11g to an audience of database professionals. Whether you are a DBA [...]
OOW 2009: The killer feature of Oracle Database 11gR2 – Edition Based Redefinition (or database object versioning)
13/10/2009 - 1:17 pm
Tags: 11gr2, Database, ebr, edition based redefinition, Oracle, plsql, universe, upgrade, versioning
Posted in Database, Devel. + PL/SQL tools, General, Oracle | 3 comments
Today I presented on what is possibly the hottest story on the Oracle Database 11gR2 release: Edition Based Redefinition (EBR). EBR allows us to add a whole new dimension to the database – the Edition (that complements the existing dimensions of schema and object type). Every database object (well, almost every database object – not [...]
Oracle RDBMS 11gR2 – new style hierarchical querying using Recursive Subquery Factoring
29/9/2009 - 5:22 pm
Tags: connect by, hierarchical query, recursive subquery factoring, sql
Posted in Data Warehousing & BI, Devel. + PL/SQL tools, General, Oracle | No comments
Oracle Database 11g Release 2 introduces the successor to the good old Connect By based hierarchical querying, called Recursive Subquery Factoring. The basics are described in a previous article: http://technology.amis.nl/blog/6104/oracle-rdbms-11gr2-goodbye-connect-by-or-the-end-of-hierarchical-querying-as-we-know-it. This article will show some additional examples of using this recursive subquery factoring syntax.
The essence of this recursiveness: the subquery consists of two queries unioned [...]
Introducing Oracle 11gR2 Edition Based Redefinition or: On Parallel Application Universes
24/9/2009 - 6:37 am
Tags: 11gr2, availability, Database, ebr, edition based redefinition, editions, planned downtime, release, versions
Posted in Database, Databases, General, J(2)EE/Java, Oracle | No comments
One of the most spectacular new facilities in Release 2 of the Oracle 11g Database is called Edition Based Redefinition – not a name perhaps that suggests any spectacle. EBR (Edition Based Redefinition) is a mechanism that allows on line application upgrade with no planned downtime. In short, the new release is built up in [...]
What You Always Wanted to Know (but never dared to ask about…)
For "What you always wanted to know, but never dared to ask about Oracle 11g Release 2…", come and attend our technical session on 29th of September during the "AMIS Query – Technical Introduction of Oracle Database 11g Release 2".
As said on the (dutch) invite: :"We will be organizing on Tuesday the 29th of September [...]
Oracle Database 11gR2 – New analytical function NTH_VALUE
15/9/2009 - 9:02 am
Tags: 11gr2, analytical functions, nth_value, sql
Posted in Data Warehousing & BI, Database, Databases, General, Oracle | No comments
You are probably familiar with the FIRST_VALUE and LAST_VALUE analytical functions that were introduced some time ago into the Oracle RDBMS, in the 9iR2 release I believe (or at least that is when they made their way into the Standard Edition). These values are used to find the first respectively last value in a window [...]
Oracle SQL and PL/SQL: juggling with user defined types (ADT) and XMLType – for creating an ADT based XML Service API
4/9/2009 - 1:37 pm
Tags: adt, plsql, service api, sql, user defined type, xmltype, xpath
Posted in Database, Devel. + PL/SQL tools, General, Oracle | 2 comments
Packages in the Oracle Database are a fine construct to use for creating a service API at PL/SQL level – that through JDBC or other connections into the database can quickly be exposed at other levels than just internally for PL/SQL. A service API has a number of characteristics, that typically include a structured, well [...]
Oracle RDBMS 11gR2 – alter or replace user defined types even when there are dependencies
2/9/2009 - 4:18 pm
Tags: 11gr2, adt, alter type, rdbms, replace type force, types
Posted in Database, Devel. + PL/SQL tools, General, Oracle | 2 comments
In our series on the 11g R2 release of the Oracle RDBMS, AMIS staff discusses various functions, features an facilities that have been added, improved or extended in this latest release. These articles are the fruit of several months of studying this new release and projecting which new features and changes would be the most [...]



2/9/2009 - 10:11 am
There are some limitations on using this [COLLECT] operator: it does not support an order by especially for the collected values
Really?
select collect(ename order by ename) from emp
seems to be a valid syntax
2/9/2009 - 1:30 pm
Oops, in that case I have overlooked that particular feature in the COLLECT operator. And I stand corrected. That is the risk you run with such a sophisticated audience! Will we meet at OOW Laurent? Thanks for your comment! Best regards, Lucas
2/9/2009 - 5:23 pm
No, I have no plan to go to OOW this year… did you submit some trivia questions to Lilian?
Kind regards
5/9/2009 - 11:54 pm
The possibility to use the collect function in combination unique or distinct or order by is new Oracle 11gr1 . I believe it isn’t possible in Oracle 10.
I hoped that listagg would return a clob instead of a varchar2. What happens when the 4000 limit is surpassed? I can’t test it because I don’t have Linux so I can’t install 11gr2 yet.
4/12/2009 - 8:11 pm
Hi Lucas,
That was good examples and feature. But how can you implement below same thing in 10g using collect or any other method
select deptno
, avg(sal) avg_sal
, listagg( ename, ‘,’) within group (order by sal)
enames
from emp
group
by deptno
/