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
Solved

Select Query

Posted on 2002-05-22
6
314 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow SQL query 12 61
Update from TABLE-A to TABLE-B 5 56
How to join on ID, with prefix? 15 65
T-SQL: New to using transactions 9 46
In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
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…

789 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