ARRAY_UNION

All functions > ARRAY > ARRAY_UNION

Returns the union of two arrays, eliminating duplicate elements.

Signatures

Returns: An array containing all unique elements from both input arrays

ARRAY_UNION(array1: ARRAY<T>, array2: ARRAY<T>) → ARRAY<T>
sql
ParameterTypeRequiredDescription
array1ARRAY<T>YesThe first input array
array2ARRAY<T>YesThe second input array

Notes

  • Combines all unique elements from both arrays
  • Eliminates duplicate elements using set semantics
  • Result is sorted lexicographically
  • Works with arrays of any comparable type
  • Empty arrays are handled gracefully

Examples

FeatureQL
SELECT
    f1 := ARRAY_UNION(ARRAY[1, 2, 3], ARRAY[2, 3, 4]),  -- Basic union with numeric arrays
    f2 := ARRAY_UNION(ARRAY['apple', 'banana'], ARRAY['banana', 'cherry']),  -- String arrays with duplicates
    f3 := ARRAY_UNION(ARRAY['A', 'B', 'C'], ARRAY['B', 'C', 'D']),  -- Multiple overlapping elements
    f4 := ARRAY_UNION(ARRAY[1, 1, 2], ARRAY[2, 2, 3]),  -- Duplicates within arrays eliminated
    f5 := ARRAY_UNION(ARRAY[1, 2], ARRAY[3, 4]),  -- No overlapping elements
    f6 := ARRAY_UNION(ARRAY['Z', 'A'], ARRAY['B', 'Y']),  -- Result is sorted lexicographically
    f7 := ARRAY_UNION(ARRAY[]::BIGINT[], ARRAY[1, 2])  -- Empty array union
;
Result
f1 ARRAYf2 ARRAYf3 ARRAYf4 ARRAYf5 ARRAYf6 ARRAYf7 ARRAY
[1, 2, 3, 4][apple, banana, cherry][A, B, C, D][1, 2, 3][1, 2, 3, 4][A, B, Y, Z][1, 2]

Last update at: 2026/03/03 16:47:38
Last updated: 2026-03-03 16:48:19