In my case, I have two table with same structure: TableA
& TableB
, and what I was trying to do is to find if there is any records only exists in A but not B.
My script was
SELECT * FROM TableA
WHERE NOT EXISTS (
SELECT * FROM TableB
)
While there is 2 records which only exists in A but not B, this script returns nothing. Then I changed into following:
SELECT ID FROM TableA
WHERE ID NOT IN (
SELECT ID FROM TableB
)
This script works successfully and return the 2 records' ID.
My question is: Is this behavior normal? What is the mechanism behind NOT EXISTS
and NOT IN
?
I have read some other posts comparing NOT EXISTS
and NOT IN
, and most people suggest using NOT EXISTS
in 99.9% scenarios, is this case fall into that 0.1% which NOT EXISTS
is not applicable? (I believed it's due to my wrongly usage though, please correct me if that's the case)