Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to find the rows in one table that are not in

Author  Topic 

kirknew2SQL
Posting Yak Master

194 Posts

Posted - 2007-11-19 : 15:50:26
How to find the rows in one table that are not in another?

I have two tables NewColors and OldColors. Both tables have a column named Color.

NewColors has 4 rows
Black
Blue
Red
Green

OldColors has 5 rows
Black
Tan
Pink
Red
Orange
Green

I need to identify that “Blue” is missing from OldColors when compared to NewColors. Then add it as a new row to OldColors. The way I was going to approach this is to full join the two tables together. Then append to OldColors the row that does not have a match.

But I am thinking there had got to be a better way, simpler way. What is the correct way to do this?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-11-19 : 15:59:15
select * from Table1 t1
where not exists (select * from Table2 t2 where t2.SomeColumn = t1.SomeColumn)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

kirknew2SQL
Posting Yak Master

194 Posts

Posted - 2007-11-19 : 16:07:36
Thank you. I knew there had to be a better way.
Go to Top of Page
   

- Advertisement -