[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Select Query

Posted on 2002-05-22
6
Medium Priority
?
339 Views
Last Modified: 2012-06-27
Hello!

My name is Assaf Lasry and I'm a junior programmer and I have a problem building a query. I'm using SQL Server and I have 2 tables (TB1, TB2). in both tables, I have a field called IDNum. I want to build a query that selects all ID's in TB1 that are NOT found in TB2. Example: If in both tables I have the following records:

TB1: IDNum          TB2: IDNum
      1                   1
      2                   4
      3                  
      4
we can see that 2 and 3 are not in TB2. These are the values that I want to retrieve. I was wondering if you could help me and tell me what should my query look like. Please, I would appreciate your help.

Thank you in advance,
Assaf Lasry.
0
Comment
Question by:AssafL
[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
6 Comments
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7027823
Assafl we need to do an outer join checking for null in TB2:

Select TB1.IDNum
From TB1 Left Outer Join TB2
    ON TB1.IDNum = TB2.IDNum
WHERE TB2.IDNum Is Null

That should do it for ya . . . =)
0
 
LVL 22

Accepted Solution

by:
DrSQL earned 600 total points
ID: 7027853
Assaf,

  Normally, I would be a little more direct than spcmnspff:

select IDNum from TB1
where not exists (select 1 from TB2 where IDNum = TB1.IDNum);

  You could also use relational operations to accomplish the same thing:

select IDNum from TB1
MINUS
select IDNum from TB2;

Both are pretty clear as to what they're trying to do and are about as fast as outer joining and looking for the unmatched records.

Good luck!
0
 
LVL 5

Expert Comment

by:spcmnspff
ID: 7027987
Does SQL server support relational notation?
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 22

Expert Comment

by:DrSQL
ID: 7028062
spcmnspff,
  Sorry about that, no it doesn't suppport MINUS (at least thru v7.0).  It only has the UNION operator.  Microsoft recommends "NOT EXISTS" instead.

Good luck!
0
 
LVL 1

Expert Comment

by:thegroup
ID: 7044838
I recomend that option

Select TB1.IDNum
From TB1 Left Outer Join TB2
   ON TB1.IDNum = TB2.IDNum
WHERE TB2.IDNum Is Null

because exists (select...) is very slow in comparision because every record in TB1 triggers a Select on TB2.
0
 
LVL 54

Expert Comment

by:nico5038
ID: 7265809

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
 - Answered by: spcmnspff  
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 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