Either of these 2 queries might solve your problem:select a.id from table_ed as a where a.stat='D' and a.year in (2010,2011) and a.transfer_date is null and not exists (select b.id from table_ed as b where b.stat<>'D' )union allselect a.id from table_p as a where a.year in (2010,2011) and not exists (select b.id from table_p as b where b.year not in (2010,2011) )
orselect id from table_ed group by id having sum(case when stat='D' and year in (2010,2011) and transfer_date is null then 0 else 1 end)=0union allselect id from table_p group by id having sum(case when year in (2010,2011) then 0 else 1 end)=0
These queries were not tested, so there might by type/syntax errors.