Postgresql – Select from arrays in a way similar to IN

arraypostgresql

I have this table and data:

CREATE TABLE sal_emp (
    name            text,
    pay_by_quarter  integer[]
);

INSERT INTO sal_emp
VALUES
   ('Bil'  , '{1, 2, 3, 4}'),
   ('Bill2', '{5, 6, 7, 8}');

I can run query:

select name from sal_emp where pay_by_quarter @> ARRAY[1,4];

And got "Bil".

Is there a way to run a query like:

select name from sal_emp where pay_by_quarter @> ARRAY[1,4,5]

and get "Bil", "Bill2"?

Best Answer

You can use overlap (&&) operator which means "have elements in common".

SELECT
    name
  FROM sal_emp
  WHERE
    pay_by_quarter && ARRAY[1,4,5]

Documentation link is here: Array Operators