Solved

Select Query

Posted on 2002-05-22
6
288 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
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 150 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now