1

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"?

1
  • The desired functionality is not properly defined in your question. Do you want the resulting arrays cover all elements of the array together? Or just any overlapping row like in the currently accepted answer? Please clarify. Jul 5, 2017 at 2:53

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.