SQL Syntax for LISTAGG in Oracle
The LISTAGG function in Oracle is used to aggregate values from multiple rows into a single string, separated by a specified delimiter. Here's the syntax and explanation:
Basic Syntax:
LISTAGG(expression, delimiter) WITHIN GROUP (ORDER BY order_by_expression)
Explanation:
expression: The column or expression whose values you want to concatenate. This is the data you want to combine into a single string.
delimiter: The string that will be used to separate the concatenated values. For example, a comma (,), a semicolon (;), or a pipe (|).
WITHIN GROUP (ORDER BY order_by_expression): This clause is mandatory. It specifies the order in which the values should be concatenated. Without the ORDER BY clause, the order of the concatenated values is undefined and can vary.
order_by_expression: The column or expression used to sort the values before concatenation. This determines the order in which the values appear in the final string.
Example:
Let's say you have a table called employees with the following data:
| employee_id | department |
|---|
| 1 | Sales |
| 2 | Marketing |
| 3 | Sales |
| 4 | IT |
| 5 | Marketing |
To get a comma-separated list of departments for each employee, you could use the following query:
SELECT
employee_id,
LISTAGG(department, ', ') WITHIN GROUP (ORDER BY department) AS departments
FROM
employees
GROUP BY
employee_id;
This would produce the following result:
| employee_id | departments |
|---|
| 1 | Sales |
| 2 | Marketing |
| 3 | Sales |
| 4 | IT |
| 5 | Marketing |
Example with a different delimiter and ordering:
To get a semicolon-separated list of departments, ordered by employee ID:
SELECT
employee_id,
LISTAGG(department, ';') WITHIN GROUP (ORDER BY employee_id) AS departments
FROM
employees
GROUP BY
employee_id;
Important Considerations:
- Maximum Length:
LISTAGG has a maximum output string length of 4000 bytes for VARCHAR2 and 32767 bytes for CLOB. If the concatenated string exceeds this limit, you'll get an error. To handle longer strings, you can use LISTAGG with a CLOB data type (see below).
- NULL Values: By default,
LISTAGG ignores NULL values. If you want to include NULL values, you can use NVL or COALESCE to replace them with a string value.
- GROUP BY Clause:
LISTAGG is an aggregate function, so you must use it with a GROUP BY clause to specify the grouping criteria.
- Oracle Version:
LISTAGG was introduced in Oracle 11g Release 2. If you're using an older version of Oracle, you'll need to use alternative methods to achieve the same result (e.g., using WM_CONCAT, which is undocumented and not recommended).
Using LISTAGG with CLOB for longer strings:
If you anticipate that the concatenated string might exceed the 4000-byte limit for VARCHAR2, you can use a CLOB data type:
SELECT
employee_id,
LISTAGG(department, ', ') WITHIN GROUP (ORDER BY department) AS departments
FROM
employees
GROUP BY
employee_id;
In this case, the departments column will be of type CLOB, allowing it to store longer strings. Note that you might need to use DBMS_LOB.SUBSTR to extract a portion of the CLOB if you need to display it in a limited-size column.
Handling NULL values:
SELECT
employee_id,
LISTAGG(NVL(department, 'N/A'), ', ') WITHIN GROUP (ORDER BY department) AS departments
FROM
employees
GROUP BY
employee_id;
This example replaces any NULL values in the department column with the string 'N/A' before concatenation.
In summary:
LISTAGG is a powerful and convenient function for concatenating values from multiple rows into a single string in Oracle. Remember to include the WITHIN GROUP (ORDER BY ...) clause and be aware of the maximum string length limitations. Using CLOBs and handling NULL values appropriately will help you avoid common issues.