Search code examples
sqlarrayspostgresqlcontains

Checking if array is contained in another array in PostgreSQL


There is an array[10,20] and I want to know if it is a subset of array[20,30,10] or not. Is there a postgresql function or operation to determine if one array contains another?

Desired outcomes:

[10,20] and [30,20,10] - true
[10,20] and [10,30]    - false
[10,20] and [20,10]    - true
[10,20] and [10,20]    - true

Solution

  • Like this,perhaps:

    SELECT ARRAY[10,20]  <@ ARRAY[30,20,10];
    

    http://www.postgresql.org/docs/current/static/functions-array.html