select distinct studentid from tab2 t where (select count(*) from tab1 where teacherid = 1) =
(select count(*) from tab2 where subjectid in (select subjectid from tab1 where teacherid = 1)
 and studentid = t.studentid
);