PDA

View Full Version : select all elements in one table that do not appear in another table


lli2k5
10-25-2006, 01:55 AM
Say that I have a table A with column colA and table B with colB.

How do I select all elements of colA in table A that are not in colB of table B?

Fumigator
10-25-2006, 06:02 AM
SELECT colA
FROM tableA as a
WHERE NOT EXISTS (SELECT * FROM tableB as b WHERE b.colB = a.colA)

miller
10-25-2006, 06:03 AM
SELECT A.colA
FROM A
LEFT JOIN B ON B.colB=A.colA
WHERE B.colB IS NULL;


Yes, I know it looks wierd, but this is what you want. Please note, this may take a while if colB is not indexed and the tables are large.