Solved

Simple SQL join to exclude records question.

Posted on 2011-02-14
10
665 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
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!

 
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

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

622 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