create table #temp1(StudentLocalID varchar(9),EnrollSchoolCode Varchar(3))create table #temp2(StudentLocalID varchar(9),ScheduleSchoolCode varchar(3))insert into #temp1 values ('003682153','305')insert into #temp1 values('003682153','303')insert into #temp1 values('003744930','305')insert into #temp1 values('003744930','332')insert into #temp2 values('003682153','305')insert into #temp2 values('003682153','303')insert into #temp2 values('003744930','305')insert into #temp2 values('003744930','303')insert into #temp2 values('003744930','332')
StudentLocalID ScheduleSchoolCode003744930 303
Select * from #temp2EXCEPTSelect * from #temp1
Select t2.*from #temp2 t2Left Join #temp1 t1 on t2.StudentLocalID=t1.StudentLocalID and t2.ScheduleSchoolCode=t1.EnrollSchoolCodewhere t1.EnrollSchoolCode is Null
Select t2.*from #temp2 t2Where NOT EXISTS (Select 1 from #temp1 t1 Where t1.EnrollSchoolCode=t2.ScheduleSchoolCode and t1.StudentLocalID=t2.StudentLocalID)