Search code examples
postgresqlpostgresql-15

Select all rows that fully intersect with the input array?


Consider the following table

id int lang_codes text[]
1 {eng,fre}
2 {eng}

I need to select all the rows from the table that intersect with the input array, but only when all the items in the lang_codes are present in the input array, e.g.:

  • if the input array contains [eng,fre,ger] then it returns all the rows
  • while [eng,ger] won't return the first record, because it needs both codes to be present in the input array.

I've tried the && operand, but it returns all the rows:

select * from my_table where lang_codes && ARRAY['eng','ger'];

From the other hand @> operand, returns only when array matches fully


Solution

  • The <@ operator should do the trick:

    select * from my_table where lang_codes <@ ARRAY['eng','ger'];
    

    SQL Fiddle