Solved

SQL Select | Join tables where data not in second table

Posted on 2009-07-07
2
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

729 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