For comparing arrays in Postgress, there are array operators.
Find the Operators section here: https://www.postgresql.org/docs/current/functions-array.html
A quick way to compare two arrays is by doing so,
( (ARRAY_1 @> ARRAY_2) AND (ARRAY_1 <@ ARRAY_2) )
which basically checks if all the unique values in ARRAY_2 is present in ARRAY_1 AND
vice versa.
Don't forget to use both the checks/conditions/comparison in conjunction(AND
)
These are called containment operators which checks all the unique values in one array and compares it with the other. They are also called as bird-operators because the operator @>
AND <@
resembles the head of a bird.
The way it works is as follows:
ARRAY_1 <@ ARRAY_2
// Check/Direction: Left-to-Right
// Checks if all the unique elements in ARRAY_1 is present in ARRAY_2 as well. If yes, it will return TRUE
// Please note this will return TRUE even if ARRAY_2 has extra elements that are not present in ARRAY_1
Similarly,
ARRAY_1 @> ARRAY_2
// Note we have changed the operator to '@>' from '<@'. Check/Direction: Right-to-Left
// Checks if all the unique elements in ARRAY_2 is present in ARRAY_1 as well. If yes, it will return TRUE
// Please note this will return TRUE even if ARRAY_1 has extra elements that are not present in ARRAY_2
So even if the order of elements are different it, will still work.
A simple way to not get confused and remember this is by looking at the >
OR <
sign.
The ARRAY(unique elements) which has the less-than sign pointed is being compared with the ARRAY(unique elements) with greater-than sign pointed.
So the ARRAY which has the greater-than sign pointed can contain extra unique elements, and still return TRUE
.
Let us try some examples
SELECT array[1, 2, 3] @> array[1, 2, 3] AS RESULT
!-- Returns TRUE (Check is Right-to-Left)
SELECT array[1, 2, 3] @> array[3, 1, 2] AS RESULT
!-- Returns TRUE (Check is Right-to-Left)
SELECT array[1, 2, 3, 3, 3, 3] @> array[1, 2, 3] AS RESULT
!-- Returns TRUE (Check is Right-to-Left)
SELECT array[1, 2, 3, 3, 3, 3] <@ array[3, 2, 1, 3, 2, 5] AS RESULT
!-- Returns TRUE (Check is Left-to-Right)
SELECT array[1, 2, 3, 4] <@ array[1, 2, 3] AS RESULT
!-- Returns FALSE (Check is Left-to-Right)
SELECT array[1, 2, 3, 4] @> array[1, 2, 3] AS RESULT
!-- Returns TRUE (Check is Right-to-Left)
SELECT array[1, 2, 3] <@ null AS RESULT
!-- Returns empty (Check is Left-to-Right)
SELECT array[1, 2, 3] @> null AS RESULT
!-- Returns empty (Check is Right-to-Left)
SELECT null <@ null AS RESULT
!-- Doesn't work if both the values are null - Error: Operator is not unique: unknown <@ unknown
SELECT array[1, 2, 3] <@ array[] AS RESULT
!-- Doesn't work on empty arrays - Error: Cannot determine type of empty array
SELECT array[] <@ array[] AS RESULT
!-- Doesn't work on empty arrays - Error: Cannot determine type of empty array
Please note
- This is used for comparing arrays.
- Works by extracting unique values from both the arrays - and then compares them.
- It does not check the count of elements in the ARRAY. i.e it can return TRUE even if both the arrays are of different length (
[1, 2] @> [1]
OR [1, 1] @> [1]
are still TRUE
)
- Doesn't work if both the values are
NULL
- Doesn't work if either of the array is empty (
[]
)