skip to Main Content

I have an array['a', 'b', 'c']

Very simply I want to check whether all elements of the above array exist in a column say test of a table t.

It would be best if the sql return boolean.

Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    so I have done it in the following way

    SELECT array_agg(DISTINCT test) @> ARRAY ['a', 'b', 'c']::varchar[] FROM t;
    

    please do suggest if there is other optimum way of doing it


  2. You can use Postgresql UNNEST function and EXCEPT function, it is more optimal if your array is large.

    SELECT DISTINCT test from unnest(ARRAY['a', 'b', 'c']) AS data(test)
    EXCEPT
    SELECT DISTINCT test from t
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search