Joins always gets me :/ so far what I have is
SELECT *
FROM
#Table1 aa
RIGHT OUTER JOIN
#Table2 bb
ON a.FieldA = b.FieldB
Joins always gets me :/ so far what I have is
SELECT *
FROM
#Table1 aa
RIGHT OUTER JOIN
#Table2 bb
ON a.FieldA = b.FieldB
I would recommmend not exists
:
select t2.*
from #table2 t2
where not exists (select 1 from #table1 t1 where t1.fieldA = t2.fieldB)
This seems like the most straight-forward way to express what you want, and should have good performance, provided that you have indexes on #table2(fieldB)
and #table1(fieldA)
.
If you really want to do this with a join
, then you can use an anti-left join,
like so:
select t2.*
from #table2 t2
left join #table1 t1 on t1.fieldA = t2.fieldB
and t1.fieldA is null
The logic is to attempt to join both tables, and pull out records of #table2
for which the join did not succeed.