here's an example of what I'm looking for:
user_id | color |
---|---|
1 | red |
1 | yellow |
1 | blue |
2 | red |
3 | red |
4 | red |
I want to pull users who DO NOT have the color red or yellow AT ALL. Which I know is not simply:
select user_id
from table_name
where color not in ('red', 'yellow')
User 1 will still output even though they have a line item for both red and yellow. The single line item of color = blue for user 1 fits the criteria of color not in ('red', 'yellow')
, but I want to exclude users that have any line item of red or yellow.
I think something like
with CTE
(of all users that have bought red or yellow)
select user_id
from table_name
where user_id not in (select * from CTE)
will work... but is that best practice? I'm wondering if there's some way easier solution or function out there