Solved

Simple SQL join to exclude records question.

Posted on 2011-02-14
10
664 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
[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
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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 explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

734 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