Skip to content

LISTAGG

LISTAGG(measure_expr [, ‘delimiter’]) WITHIN GROUP (order_by_clause) [OVER (query_partition_clause)]

Section titled “LISTAGG(measure_expr [, ‘delimiter’]) WITHIN GROUP (order_by_clause) [OVER (query_partition_clause)]”
  • measure_expr: This parameter identifies the column or expression which you want to concatenate into a string.
  • ‘delimiter’: This optional parameter is the string that separates the values within the combined string. If you omit the ‘delimiter’, Oracle uses a null string.
  • order_by_clause: This parameter determines the order of the elements in the concatenated string made from measure_expr values. It’s used with WITHIN GROUP clause.
  • query_partition_clause: This optional parameter is part of the OVER clause. It allows you to partition the rows into groups for the LISTAGG function to act upon.
SELECT department_id,
LISTAGG(employee_id, ',') WITHIN GROUP (ORDER BY employee_id)
AS "Employee_List"
FROM employees
GROUP BY department_id;
DEPARTMENT_ID | Employee_List
--------------|----------------
10 | 200,201
20 | 202,203,204
30 | 205

The LISTAGG() function aggregates the ‘employee_id’ from the ‘employees’ table into a list separated by commas. The WITHIN GROUP clause arranges ‘employee_id’ in an ascending order before aggregation. The entire operation is grouped by ‘department_id’, resulting in a list of employees for each department.