create table T1 as
select 1 as ID,'X' as Code from dual
union all select 1,'X' from dual
union all select 1,'Y' from dual
union all select 2,'X' from dual
union all select 2,'Y' from dual
union all select 3,'X' from dual
union all select 3,'X' from dual
union all select 3,'Y' from dual
union all select 3,'Y' from dual
union all select 4,'X' from dual
union all select 4,'X' from dual
union all select 5,'Y' from dual
union all select 5,'Y' from dual;
SQL
select ID
from T1
where Code in('X','Y')
group by ID
having sum(decode(Code,'X',1,0)) = 2
and sum(decode(Code,'Y',1,0)) = 1;
解説
having句で、
sum関数とdecode関数を組み合わせてます