I have a users table with this structure:
CREATE TABLE users (
id integer NOT NULL,
email text,
account_id integer,
segment_ids integer[]
);
And a segments table with this structure:
CREATE TABLE segments (
id integer NOT NULL,
name text
);
I have built a query like this to find all the users which belong to segments:
SELECT * FROM users
WHERE segment_ids @> (
SELECT ARRAY(
SELECT id from segments
WHERE name IN ('Melbourne', 'Male', 'Team #1')
)
)
However, I am not satisfied that using a subquery is the best way to accomplish this. Is there a cleaner way of writing this query, i.e. with a joins, or is a subquery the way to go?