Tuesday, November 09, 2004

sql: rows in one table but not in another

This comes up a lot in database programming. How do you write a SQL query that selects rows that are in one table but not in another table? Here's the answer:

select a.field0
from table0 a
where not exists
        (select b.field0
         from table1 b
         where b.field0 = a.field0)

2 Comments:

At 3:47 PM, Blogger E Perrott said...

Thanks, randomly got this in a google search and it helped us with some voter info in Florida.

 
At 11:41 AM, Blogger Louis Parsonson said...

Thanks also, just found this via google and it fixed my conundrum.

Greetings from Nottingham, UK, by the way :)

 

Post a Comment

<< Home