Skip to content
Advertisement

SQL Join that finds non matches

I have two tables “SellerDetails” and “ANL” and both have key fields of “Seller Ref No” and “Sales Year”.

enter image description here

This giving me where there is a match on Seller Ref No where the Sales Year is “20” and the Department is “Remote Sales”

But how do I find out the records that are in “SellerDetails” but NOT in “ANL” where the Sales Year is “20” and the Department is “Remote Sales”? I have tried this but it returns a quantity much larger than I expect. I have also tried changing ‘=’ to ‘<>’ but that provided a worse result

Advertisement

Answer

You can use not exists:

It is unclear whether you want to use [Sales Year] as a correlation condition too: it is present in both tables, but your query does not use that relation. If you want it, then:

Side note: here is the query you presumably wanted to write, using a left join:

The last condition in the WHERE clause filters out matching rows.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement