Here’s a simple query:
SELECT t1.ID
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL
The key points are:
-
LEFT JOINis used; this will return ALL rows fromTable1, regardless of whether or not there is a matching row inTable2. -
The
WHERE t2.ID IS NULLclause; this will restrict the results returned to only those rows where the ID returned fromTable2is null – in other words there is NO record inTable2for that particular ID fromTable1.Table2.IDwill be returned as NULL for all records fromTable1where the ID is not matched inTable2.