Solved

Simple SQL join to exclude records question.

Posted on 2011-02-14
10
663 Views
Last Modified: 2012-06-21
I have two tables -

T1
T1.id(int)
T1.name(string)

T2
T2.id(int)

I want to select all records in table T1 where name = “Smith” but exclude all those records where t1.id = t2.id.

In other words, If T1.name=”Smith” has an ID of 222, and T2.ID = 222 exists, I do not want to return the record.

SELECT * FROM T1, T2 WHERE T1.NAME = “SMITH” AND T1.ID != T2.ID
Doesn’t work.

Thanks
0
Comment
Question by:sabev
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 125 total points
ID: 34889066
Try
SELECT * FROM T1 WHERE ID NOT IN (SELECT ID FROM T2)

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34889077
Modified for Smith
SELECT * FROM T1 WHERE NAME = 'SMITH' 
AND ID NOT IN (SELECT ID FROM T2) 

Open in new window

0
 
LVL 19

Expert Comment

by:nschafer
ID: 34889084
Try this:

select * from t1 where Name = 'Smith' and id not in (Select id from t2)

Hope this helps,

Neal.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 19

Expert Comment

by:nschafer
ID: 34889100
Guess we all had the same idea at the same time :-)
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34889111
:)
0
 
LVL 11

Expert Comment

by:rajvja
ID: 34889139
SELECT * FROM T1
left join T2 on t1.Id = T2.ID
where T1.Name = 'Smith' AND t2.ID IS NULL
0
 
LVL 7

Expert Comment

by:waltersnowslinarnold
ID: 34889150
You can also use the below sample, more optimized way
SELECT * FROM T1 INNER JOIN T2
ON t2.ID = t1.ID 
WHERE Name = 'Smith' AND t2.ID <> t1.ID

Open in new window

0
 
LVL 7

Expert Comment

by:waltersnowslinarnold
ID: 34889181
Oops! @rajvja is correct, it should be LEFT OUTER JOIN
SELECT * FROM T1
left join T2 on t1.ID = T2.ID
where T1.Name = 'Smith' AND t2.ID IS NULL

Open in new window

0
 
LVL 18

Expert Comment

by:deighton
ID: 34889773
SELECT * FROM T1 WHERE T1.NAME = 'SMITH'
AND  NOT EXISTS  (SELECT NULL FROM T2 WHERE T1.ID = T2.ID)
0
 

Author Closing Comment

by:sabev
ID: 34889796
Lots of good answers, but this one was first.  Hard to decide really.

And, my apologies.  There is something strange going on, this is the second time that I have accepted this answer.  Hope it works this time.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 the fundamental information of how to create a table.

830 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