ARRAY_INTERSECTION
functionsupportedCore SQL functionsReturns an array that contains the matching elements in the two input arrays. The function is NULL-safe, meaning it treats NULLs as known values for comparing equality.
Syntax
ARRAY_INTERSECTION( <array1> , <array2> )
Parameters
| Name | Required | Description |
|---|---|---|
array1 | required | An array that contains elements to be compared. |
array2 | required | An array that contains elements to be compared. |
Returns
This function returns an ARRAY that contains the elements of the input arrays that match. If no elements overlap, the function returns an empty array. If one or both arguments are NULL, the function returns NULL. The order of the values within the returned array is unspecified.
Usage notes
- When comparing data of type OBJECT, the objects must be identical to be considered matching. For details, see Examples (in this topic).
- The difference between ARRAY_INTERSECTION and the related ARRAYS_OVERLAP function is that the ARRAYS_OVERLAP function simply returns TRUE or FALSE, while ARRAY_INTERSECTION returns the actual overlapping values.
- In Snowflake, arrays are multi-sets, not sets. In other words, arrays can contain multiple copies of the same value. ARRAY_INTERSECTION compares arrays by using multi-set semantics (sometimes called “bag semantics”), which means that the function can return multiple copies of the same value. If one array has N copies of a value, and the other array has M copies of the same value, then the number of copies in the returned array is the smaller of N or M. For example, if N is 4 and M is 2, then the returned value contains 2 copies.
- Both arguments must either be structured ARRAYs or semi-structured ARRAYs.
- If you are passing in structured ARRAYs: The function returns an ARRAY of a type that can accommodate both input types.
- The ARRAY in the second argument must be comparable to the ARRAY in the first argument.