How to exclude rows that don't join with another table?

Chaddeus picture Chaddeus · Dec 30, 2010 · Viewed 107.4k times · Source

I have two tables, one has primary key other has it as a foreign key.

I want to pull data from the primary table, only if the secondary table does not have an entry containing it's key. Sort of an opposite of a simple inner join, which returns only rows that join together by that key.

Answer

Pranay Rana picture Pranay Rana · Dec 30, 2010

alt text

SELECT <select_list> 
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL

Full image of join alt text

From aticle : http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx