Solved

SQL Select | Join tables where data not in second table

Posted on 2009-07-07
2
415 Views
Last Modified: 2012-05-07
SQL 2005
To simplify what I want to do...
I have table1 and table2.  I want to select the records in table1 that are not in table2.  I know I can do this by the following:
select t1.name, t1.address from t1 where t1.id not in (select t2.id from t2)

But is there a faster way?  The only field in t2 is "id".
0
Comment
Question by:ClowWater
2 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 100 total points
ID: 24795786
select t1.name, t1.address from t1
where NOT EXISTS (SELECT  1 FROM  t2 where t2.id = t1.id )

and sometimes LEFT JOINS are faster, but it depends
0
 
LVL 17

Accepted Solution

by:
pssandhu earned 150 total points
ID: 24795795
You can do it this if you are looking for a different way of doing this:
Select t.*
from  Table1 t LEFT OUTER JOIN Table2 c
           ON t.ID = c.ID
Where c.ID IS NULL
P.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question