Use these below. The queries are accurateSELECT distinct BELNR FROM BSIK aWHEREexists (SELECT * FROM SA_BSIK aa where aa.xref2 = a.Bsik)SELECT distinct BELNR FROM BSIK aWHEREnot exists (SELECT * FROM SA_BSIK aa where aa.xref2 = a.Bsik)
Here's a sampleIF OBJECT_ID('tempdb..#Tmp1') IS NOT NULL Begin drop table #Tmp1EndIF OBJECT_ID('tempdb..#Tmp2') IS NOT NULL Begin drop table #Tmp2Endselect 1 as idInto #tmp1Union allselect 2Union allselect 3Union allselect 4Union allselect 5select 6 as idInto #tmp2Union allselect 7Union allselect 3Union allselect 4Union allselect 5SELECT distinct ID as [ID's In Both Tables]FROM #tmp1 aWHEREexists (SELECT * FROM #Tmp2 aa where aa.ID = a.ID)SELECT distinct ID as [ID's not in Tmp2]FROM #tmp1 aWHEREnot exists (SELECT * FROM #Tmp2 aa where aa.ID = a.ID)