「"aaa"と"zzz"のみ含んだuser_id」の解釈が微妙ですが、「aaa を含む code と zzz を含む code の両方を持ち、それ以外の code (bbb や ccc など) を持たない user_id」ということなら、こんな感じでしょうか。
sql
1create table "table" (id int primary key, user_id int, code text);
2
3insert into "table" values (1, 10, 'aaa1');
4insert into "table" values (2, 10, 'bbb1');
5insert into "table" values (3, 11, 'aaa2');
6insert into "table" values (4, 11, 'bbb2');
7insert into "table" values (5, 12, 'aaa3');
8insert into "table" values (6, 12, 'bbb3');
9insert into "table" values (7, 12, 'ccc3');
10insert into "table" values (8, 13, 'aaa4');
11insert into "table" values (9, 14, 'aaa5');
12insert into "table" values (10, 14, 'zzz1');
13
14insert into "table" values (11, 15, 'aaa6');
15insert into "table" values (12, 15, 'bbb6');
16insert into "table" values (13, 15, 'zzz6');
sql
1select distinct user_id
2from "table"
3where user_id in (select user_id from "table" where code like '%aaa%')
4 and user_id in (select user_id from "table" where code like '%zzz%')
5 and user_id not in (select user_id from "table" where code not like '%aaa%' and code not like '%zzz%');
なお、aaazzz があるとそれも引っかかりますね…。
sql
1insert into "table" values (14, 16, 'aaazzz');
追記。intersect と except でもいけますね。
sql
1(select user_id from "table" where code like '%aaa%'
2 intersect
3 select user_id from "table" where code like '%zzz%')
4except
5select user_id from "table" where code not like '%aaa%' and code not like '%zzz%';