Postgres WHERE two arrays have a non-empty intersection

Viewed 1923

I have a table with a column tags of type varchar [].

I would like to select all rows where the tags contains at least one of a set of values.

Something like this:

-- Not real code
SELECT * 
FROM foo 
WHERE non_empty(set_intersection(tags, '{ "apples", "bananas", "cherries" }'))

What is the syntax for this?


I know I can do a series of ORs but that seems less elegant.

1 Answers

You can use &&, the array overlap operator:

select *
from foo
where tags && ARRAY['apples', 'bananas', 'cherries']

From the documentation:

&&: overlap (have elements in common)

Related