Please review this statement:
SELECT TableID FROM Table t1
INNER JOIN BlackList b ON b.TableID <> t1.TableID
I was thinking this statement returned everything from Table that wasn’t found in the Blacklist table, but instead it returned nothing at all (0 rows). If I’m trying to return everything from Table that IS NOT found in the Blacklist table, what’s the best way to do this? I assume you can do this:
SELECT TableID FROM (
SELECT TableID, CASE WHEN b.TableID IS NULL THEN 1 ELSE 0 END OnBlackList
FROM Table t1
LEFT JOIN Blacklist b ON b.TableID = t1.TableID
) tb1
WHERE tb1.OnBlackList = 0
But I was looking for a shorter, more efficient solution. Any suggestions?
One basic way is using a
NOT EXISTS:This will select rows in
t1that are not present in theBlackListtable.