General list aggregate functions
Use these functions to run an aggregate function, such as sum or max, over the elements of one list instead of over rows. They handle NULL elements the way that the aggregate functions handle NULL rows, so most of them ignore NULL elements.
list_aggregate function
Applies the aggregate function named function_name to the elements of list, as if the elements were the rows of a group, and returns the single result. For example, list_aggregate( returns the smallest element. Any further arguments are passed to the aggregate function after the list elements, such as the separator for list, 'min')string_agg or the fraction for quantile_cont.
The aggregate function handles NULL elements as it does NULL rows, so most functions ignore them. For an empty list, the result is the aggregate's result for no rows, such as NULL for sum and 0 for count. If list is NULL, the result is NULL, even for count.
aggregate, array_aggr, array_aggregate, and list_aggr are aliases of list_aggregate.
Syntax
list_aggregate(list, function_name [, argument
[, ...] ])
Arguments
The list_aggregate function takes the following arguments:
list-
The list whose elements you want to aggregate, as a
LISTorARRAYvalue. function_name-
The name of an aggregate function, such as
'sum','max', or'string_agg'. It must be a constantVARCHARvalue. argument-
One or more further arguments for the aggregate function. This argument is optional.
Return type
The return type of the aggregate function for the element type of list, for example INTEGER for 'min' over an INTEGER list, and HUGEINT for 'sum' over an INTEGER list.
Examples
The following SQL query returns the smallest element of a list. The NULL element is ignored.
SELECT list_aggregate([1, 2, NULL], 'min') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query joins the elements of a list into one string with string_agg, and passes the separator ' | ' as a further argument.
SELECT list_aggregate( ['a', 'b', 'c'], 'string_agg', ' | ' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the list_aggr alias to sum a list of integers. The result type is HUGEINT.
SELECT list_aggr([2, 4, 8, 42], 'sum') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query counts the elements of an empty list and of a NULL list. The empty list returns 0, and the NULL list returns NULL.
SELECT list_aggregate( []::INTEGER[], 'count' ) AS empty_list, list_aggregate( NULL::INTEGER[], 'count' ) AS null_list
The query returns the following result.
| empty_list | null_list |
|---|---|
|
|
Considerations
function_name must be a constant. A column or other expression that changes from row to row makes the query fail.
Only aggregate functions work. A window-only function such as row_number, or a name that is not a function, makes the query fail.
list_approx_count_distinct function
Applies the approx_count_distinct aggregate function to the elements of list, which estimates the number of distinct values. NULL elements are not counted. It is the same as list_aggregate(.list, 'approx_count_distinct')
The result is an estimate, and for lists with many distinct values it can differ from the true count. For an exact count that also leaves out NULL elements, use len(list_distinct(. For an empty list, the result is list))0. If list is NULL, the result is NULL.
Syntax
list_approx_count_distinct(list)
Arguments
The list_approx_count_distinct function takes the following argument:
list-
The list whose distinct elements you want to count, as a
LISTorARRAYvalue of any element type.
Return type
BIGINT
Examples
The following SQL query estimates the number of distinct elements in a short list.
SELECT list_approx_count_distinct([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the NULL element is not counted, and that an empty list returns 0.
SELECT list_approx_count_distinct( ['a', NULL, 'a', 'b'] ) AS non_null_values, list_approx_count_distinct( []::VARCHAR[] ) AS empty_list
The query returns the following result.
| non_null_values | empty_list |
|---|---|
|
|
The following SQL query compares the estimate with the exact count for a list of 1,000 distinct integers. The estimate is close to the exact count, but not equal to it.
SELECT list_approx_count_distinct( generate_series(1, 1000) ) AS approximate, len( list_distinct(generate_series(1, 1000)) ) AS exact
The query returns the following result.
| approximate | exact |
|---|---|
|
|
list_avg function
Applies the avg aggregate function to the elements of list and returns their arithmetic mean. NULL elements are ignored. It is the same as list_aggregate(.list, 'avg')
If list is empty, is NULL, or has only NULL elements, the result is NULL.
Syntax
list_avg(list)
Arguments
The list_avg function takes the following argument:
list-
The list of values to average, as a
LISTorARRAYof numbers, or ofINTERVAL,TIMESTAMP, or other values thatavgaccepts.
Return type
DOUBLE for numeric elements, including DECIMAL elements. For time, timestamp, and interval elements, such as TIME, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or INTERVAL, the element type. For DATE elements, the result is a timestamp, not a DATE.
Examples
The following SQL query returns the average of a list of integers.
SELECT list_avg([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the NULL element is ignored, so the average of 1 and 3 is 2.0, and that an empty list returns NULL.
SELECT list_avg([1, NULL, 3]) AS with_null, list_avg([]::INTEGER[]) AS empty_list
The query returns the following result.
| with_null | empty_list |
|---|---|
|
|
The following SQL query returns the midpoint of two timestamps.
SELECT list_avg( [ TIMESTAMP '2026-10-01 00:00:00', TIMESTAMP '2026-10-03 00:00:00' ] ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
For a list of DECIMAL values, the result is a DOUBLE, not a DECIMAL. Cast the result if you need a DECIMAL.
A list of strings makes the query fail.
list_bit_and function
Combines the elements of list with a bitwise AND. A bit of the result is 1 only when that bit is 1 in every element. NULL elements are ignored. If list is NULL, is empty, or holds only NULL elements, the result is NULL.
list_bit_and( returns the same result as list)list_aggregate(. To combine values across rows instead of within one list, use the list, 'bit_and')bit_and aggregate function.
Syntax
list_bit_and(list)
Arguments
The list_bit_and function takes the following argument:
list-
The list to combine with bitwise AND, as a list of integers, such as an
INTEGER[]orBIGINT[]value, or a list ofBITvalues.
Return type
The element type of list. For example, an INTEGER[] list returns an INTEGER.
Examples
The following SQL query combines 3 (binary 0011), 3, and 9 (binary 1001). Only the lowest bit is set in all three values, so the result is 1.
SELECT list_bit_and([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the NULL element is ignored. The bitwise AND of 12 (binary 1100) and 10 (binary 1010) is 8 (binary 1000).
SELECT list_bit_and([12, 10, NULL]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that an empty list returns NULL.
SELECT list_bit_and( CAST([] AS INTEGER[]) ) IS NULL AS is_null
The query returns the following result.
| is_null |
|---|
|
Considerations
The elements must be integers or BIT values. A list of decimals, such as [1.5, 2.5], or a list of strings causes an error.
list_bit_or function
Combines the elements of list with a bitwise OR. A bit of the result is 1 when that bit is 1 in at least one element. NULL elements are ignored. If list is NULL, is empty, or holds only NULL elements, the result is NULL.
list_bit_or( returns the same result as list)list_aggregate(. To combine values across rows instead of within one list, use the list, 'bit_or')bit_or aggregate function.
Syntax
list_bit_or(list)
Arguments
The list_bit_or function takes the following argument:
list-
The list to combine with bitwise OR, as a list of integers, such as an
INTEGER[]orBIGINT[]value, or a list ofBITvalues.
Return type
The element type of list. For example, an INTEGER[] list returns an INTEGER.
Examples
The following SQL query combines 3 (binary 0011), 3, and 9 (binary 1001) into 11 (binary 1011).
SELECT list_bit_or([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query combines single-bit flags into one value. The NULL element is ignored, and the bitwise OR of 1 and 4 is 5 (binary 0101).
SELECT list_bit_or([1, NULL, 4]) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The elements must be integers or BIT values. A list of decimals or a list of strings causes an error.
list_bit_xor function
Combines the elements of list with a bitwise exclusive OR. A bit of the result is 1 when that bit is 1 in an odd number of elements, so two equal values cancel each other out. NULL elements are ignored. If list is NULL, is empty, or holds only NULL elements, the result is NULL.
list_bit_xor( returns the same result as list)list_aggregate(. To combine values across rows instead of within one list, use the list, 'bit_xor')bit_xor aggregate function.
Syntax
list_bit_xor(list)
Arguments
The list_bit_xor function takes the following argument:
list-
The list to combine with bitwise XOR, as a list of integers, such as an
INTEGER[]orBIGINT[]value, or a list ofBITvalues.
Return type
The element type of list. For example, an INTEGER[] list returns an INTEGER.
Examples
The following SQL query combines 3, 3, and 9. The two 3 values cancel each other out, so the result is 9.
SELECT list_bit_xor([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query combines 5 (binary 0101) and 3 (binary 0011) into 6 (binary 0110).
SELECT list_bit_xor([5, 3]) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The elements must be integers or BIT values. A list of decimals or a list of strings causes an error.
list_bool_and function
Returns true when every element of list is true, and false when at least one element is false. NULL elements are ignored, so [true, NULL] returns true. If list is NULL, is empty, or holds only NULL elements, the result is NULL, not true.
list_bool_and( returns the same result as list)list_aggregate(. To test whether every element of a list meets a condition, turn the list into Boolean values with list, 'bool_and')list_transform first, as in list_bool_and(list_transform(.list, lambda x: x > 0))
Syntax
list_bool_and(list)
Arguments
The list_bool_and function takes the following argument:
list-
The list of Boolean values to test, as a
BOOLEAN[]list.
Return type
BOOLEAN
Examples
The following SQL query returns false because one element is false.
SELECT list_bool_and([true, false]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a NULL element is ignored, and that an empty list returns NULL.
SELECT list_bool_and([true, NULL]) AS with_null, list_bool_and( CAST([] AS BOOLEAN[]) ) IS NULL AS empty_is_null
The query returns the following result.
| with_null | empty_is_null |
|---|---|
|
|
The following SQL query tests whether every element is greater than 0. The list_transform function applies the lambda function lambda x: x > 0 to each element x and returns a list of Boolean results.
SELECT list_bool_and( list_transform([1, 5], lambda x: x > 0) ) AS all_positive
The query returns the following result.
| all_positive |
|---|
|
Considerations
The elements must be BOOLEAN values. A list of integers, such as [1, 0], or a list of strings, such as ['true', 'false'], causes an error. Compare or cast the elements first.
Differences from Athena engine version 3
Athena engine version 3 has all_match(. DuckDB has no array, function)all_match function, so write list_bool_and(list_transform( instead. The two can return different results. When the condition is array, lambda x: condition))NULL for some elements and true for the rest, all_match returns NULL, but list_bool_and ignores the NULL results and returns true. For an empty array, all_match returns true, and list_bool_and returns NULL.
list_bool_or function
Returns true when at least one element of list is true, and false when every element is false. NULL elements are ignored, so [false, NULL] returns false. If list is NULL, is empty, or holds only NULL elements, the result is NULL, not false.
list_bool_or( returns the same result as list)list_aggregate(. To test whether any element of a list meets a condition, turn the list into Boolean values with list, 'bool_or')list_transform first, as in list_bool_or(list_transform(.list, lambda x: x > 4))
Syntax
list_bool_or(list)
Arguments
The list_bool_or function takes the following argument:
list-
The list of Boolean values to test, as a
BOOLEAN[]list.
Return type
BOOLEAN
Examples
The following SQL query returns true because one element is true.
SELECT list_bool_or([true, false]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a NULL element is ignored, and that an empty list returns NULL.
SELECT list_bool_or([false, NULL]) AS with_null, list_bool_or( CAST([] AS BOOLEAN[]) ) IS NULL AS empty_is_null
The query returns the following result.
| with_null | empty_is_null |
|---|---|
|
|
The following SQL query tests whether any element is greater than 4, and whether no element is greater than 9. list_transform applies each lambda function to every element x.
SELECT list_bool_or( list_transform([1, 5], lambda x: x > 4) ) AS any_over_4, NOT list_bool_or( list_transform([1, 5], lambda x: x > 9) ) AS none_over_9
The query returns the following result.
| any_over_4 | none_over_9 |
|---|---|
|
|
Considerations
The elements must be BOOLEAN values. A list of integers or a list of strings causes an error. Compare or cast the elements first.
Differences from Athena engine version 3
Athena engine version 3 has any_match( and array, function)none_match(. DuckDB has neither function. For array, function)any_match, write list_bool_or(list_transform(, and for array, lambda x: condition))none_match, put NOT in front of that expression. The results can differ. When no element matches and the condition is NULL for some elements, any_match returns NULL, but list_bool_or ignores the NULL results and returns false. For an empty array, any_match returns false, and list_bool_or returns NULL.
When you replace none_match with NOT list_bool_or(list_transform(, the results can also differ. When no element matches and the condition is array, lambda x: condition))NULL for some elements, none_match returns NULL, but the NOT list_bool_or expression returns true. For an empty array, none_match returns true, and the NOT list_bool_or expression returns NULL.
list_histogram function
Returns a MAP with one entry for each distinct element of list that is not NULL. The key is the element, and the value is the number of times that the element occurs in the list. The keys are in ascending order. NULL elements are not counted.
If list is empty, holds only NULL elements, or is NULL, the result is NULL, not an empty map.
list_histogram( returns the same result as list)list_aggregate(. To count values across rows instead of within a list, use the list, 'histogram')histogram aggregate function.
Syntax
list_histogram(list)
Arguments
The list_histogram function takes the following argument:
list-
The list whose elements you want to count, with elements of any type.
Return type
MAP(, where T, UBIGINT)T is the element type of list, for example MAP(INTEGER, UBIGINT) for an INTEGER[] list.
Examples
The following SQL query counts the elements of a list.
SELECT list_histogram([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query counts strings. The NULL element is not counted, and the keys are in ascending order.
SELECT list_histogram(['b', 'a', 'b', NULL]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses square brackets to look up the count for the key 'b' in the map that list_histogram returns.
SELECT list_histogram(['b', 'a', 'b'])['b'] AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that an empty list returns NULL, not an empty map.
SELECT list_histogram( CAST([] AS INTEGER[]) ) IS NULL AS is_null
The query returns the following result.
| is_null |
|---|
|
list_max function
Returns the largest element of list. NULL elements are ignored. If list is empty, holds only NULL elements, or is NULL, the result is NULL.
list_max( returns the same result as list)list_aggregate(. To get the smallest element, use the list_min function. To get the largest value across rows, use the list, 'max')max aggregate function.
Syntax
list_max(list)
Arguments
The list_max function takes the following argument:
list-
The list to search for the largest element, with elements of any type that can be ordered.
Return type
The element type of list, for example INTEGER for an INTEGER[] list.
Examples
The following SQL query returns the largest element of a list.
SELECT list_max([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a NULL element is ignored.
SELECT list_max([1, NULL, 5]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the latest date in a list.
SELECT list_max( [DATE '2026-10-01', DATE '2026-10-03'] ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
Strings are compared by the code values of their characters, so uppercase letters sort before lowercase letters. For example, list_max(['apple', 'Banana']) returns apple. To compare strings without regard to case, convert them first, for example with list_transform and lower.
Differences from Athena engine version 3
Athena engine version 3 uses array_max, which returns NULL when the array has a NULL element. In DuckDB, use list_max, which ignores NULL elements and returns the largest of the other elements. DuckDB has no array_max function.
list_min function
Returns the smallest element of list. NULL elements are ignored. If list is empty, holds only NULL elements, or is NULL, the result is NULL.
list_min( returns the same result as list)list_aggregate(. To get the largest element, use the list_max function. To get the smallest value across rows, use the list, 'min')min aggregate function.
Syntax
list_min(list)
Arguments
The list_min function takes the following argument:
list-
The list to search for the smallest element, with elements of any type that can be ordered.
Return type
The element type of list, for example INTEGER for an INTEGER[] list.
Examples
The following SQL query returns the smallest element of a list.
SELECT list_min([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a NULL element is ignored.
SELECT list_min([NULL, 4, 2]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that string comparison is case sensitive. Banana sorts before apple because uppercase letters come first. The second column uses list_transform with the lambda function lambda s: lower(s), which converts each element to lowercase, so the result is apple.
SELECT list_min( ['apple', 'Banana', 'cherry'] ) AS case_sensitive, list_min( list_transform( ['apple', 'Banana', 'cherry'], lambda s: lower(s) ) ) AS lowercased
The query returns the following result.
| case_sensitive | lowercased |
|---|---|
|
|
Considerations
Strings are compared by the code values of their characters, so uppercase letters sort before lowercase letters. To compare strings without regard to case, convert them first, for example with list_transform and lower.
Differences from Athena engine version 3
Athena engine version 3 uses array_min, which returns NULL when the array has a NULL element. In DuckDB, use list_min, which ignores NULL elements and returns the smallest of the other elements. DuckDB has no array_min function.
list_product function
Returns the product of the elements of list, as a DOUBLE. NULL elements are ignored. If list is empty, contains only NULL elements, or is NULL, the result is NULL.
list_product( returns the same result as list)list_aggregate(. To multiply values across rows, use the list, 'product')product aggregate function.
Syntax
list_product(list)
Arguments
The list_product function takes the following argument:
list-
The list of numeric values to multiply. The values are converted to
DOUBLE.
Return type
DOUBLE
Examples
The following SQL query multiplies the elements of a list.
SELECT list_product([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query ignores the NULL element and multiplies the other two.
SELECT list_product([2, NULL, 5]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes an empty list, so the result is NULL.
SELECT list_product(CAST([] AS INTEGER[])) AS result
The query returns the following result.
| result |
|---|
|
list_string_agg function
Returns the elements of list joined into one VARCHAR value, with a comma and no space between them. Elements that are not strings, such as numbers, are converted to VARCHAR first, and NULL elements are left out.
If list is empty, holds only NULL elements, or is itself NULL, the result is NULL, not an empty string. list_string_agg( returns the same result as list)list_aggregate(.list, 'string_agg')
Syntax
list_string_agg(list)
Arguments
The list_string_agg function takes the following argument:
list-
The list whose elements you want to join, as a
LISTvalue.
Return type
VARCHAR
Examples
The following SQL query joins three strings.
SELECT list_string_agg(['a', 'b', 'c']) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query leaves out a NULL element, and converts a list of numbers to one string.
SELECT list_string_agg(['a', NULL, 'c']) AS with_null, list_string_agg([1, 2, 3]) AS numbers
The query returns the following result.
| with_null | numbers |
|---|---|
|
|
The following SQL query shows that an empty list returns NULL from list_string_agg, and an empty string from array_to_string.
SELECT list_string_agg([]) IS NULL AS agg_is_null, array_to_string([], ',') = '' AS to_string_is_empty
The query returns the following result.
| agg_is_null | to_string_is_empty |
|---|---|
|
|
The following SQL query joins three strings with the separator ' | ' instead of a comma. It calls list_aggregate with 'string_agg' and the separator.
SELECT list_aggregate( ['a', 'b', 'c'], 'string_agg', ' | ' ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The separator is always a comma, and list_string_agg takes no separator argument. To use another separator, call list_aggregate( or list, 'string_agg', separator)array_to_string(.list, separator)
For an empty list, list_string_agg returns NULL, but array_to_string returns an empty string. To get an empty string from list_string_agg, use coalesce(list_string_agg(.list), '')
list_sum function
Adds up the elements of list that are not NULL. If list is empty, holds only NULL elements, or is itself NULL, the result is NULL, not 0. To get 0 instead, use coalesce(list_sum(.list), 0)
list_sum( returns the same result as list)list_aggregate(, and its result type follows the list, 'sum')sum aggregate function. For a list of integers, the result is a HUGEINT, a 128-bit integer, so a sum of BIGINT values does not overflow. For a list of BOOLEAN values, the result is the number of true elements.
Syntax
list_sum(list)
Arguments
The list_sum function takes the following argument:
list-
The list to sum, as a
LISTof numbers (TINYINT,SMALLINT,INTEGER,BIGINT,HUGEINT,BIGNUM,DECIMAL,FLOAT, orDOUBLE), or aLISTofBOOLEANvalues.
Return type
The result is HUGEINT for a list of BOOLEAN values or of integers up to HUGEINT, and DECIMAL(38, for a list of s)DECIMAL values with scale s. It is DOUBLE for a list of FLOAT or DOUBLE values, and BIGNUM for a list of BIGNUM values.
Examples
The following SQL query adds up a list of integers.
SELECT list_sum([3, 3, 9]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query skips a NULL element.
SELECT list_sum([1, NULL, 3]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the sum of an empty list is NULL, and uses coalesce to return 0 instead.
SELECT list_sum([]) IS NULL AS empty_is_null, coalesce(list_sum([]), 0) AS with_default
The query returns the following result.
| empty_is_null | with_default |
|---|---|
|
|
The following SQL query adds up a list of DECIMAL values, so the result is an exact DECIMAL value.
SELECT list_sum([1.25, 2.50]) AS result
The query returns the following result.
| result |
|---|
|
Considerations
list_sum does not convert strings to numbers, so a list of VARCHAR values makes the query fail. Cast the elements first, for example list_sum(list_transform(.list, lambda s: CAST(s AS DECIMAL(10,2))))
A Hive table cannot store a HUGEINT column. When you write the sum of a list of integers to a Hive table, cast it, as in CAST(list_sum(.list) AS BIGINT)