Solved

Select Query

Posted on 2002-05-22
6
299 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now