Solved

SQL Select | Join tables where data not in second table

Posted on 2009-07-07
2
421 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 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