If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Need help formulating a non-equal join

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-01-09, 23:49
lennyw lennyw is offline
Registered User
 
Join Date: Jul 2006
Posts: 3
Need help formulating a non-equal join

I'm trying to get all records in table ADRS whose zip codes are NOT present in table EXCLUDE's zip code column.

Th following gives me the exact inverse of what I want (e.g. all records in ADRS whose zip codes DO match some zip code in EXCLUDE:

SELECT ADRS.First, ADRS.Last, ADRS.Adr, ADRS.City, ADRS.St, ADRS.Zip
FROM ADRS INNER JOIN EXCLUDE ON ADRS.Zip =EXCLUDE.Zip;

BTW, ADRS.Zip is never null.

How can I turn this query "upside down"?

Any help greatly appreciated

Tnx,
Lenny Wintfeld
Reply With Quote
  #2 (permalink)  
Old 05-02-09, 06:04
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
Code:
SELECT ADRS.First
     , ADRS.Last
     , ADRS.Adr
     , ADRS.City
     , ADRS.St
     , ADRS.Zip
  FROM ADRS 
LEFT OUTER 
  JOIN EXCLUDE 
    ON EXCLUDE.Zip = ADRS.Zip
 WHERE EXCLUDE.Zip IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-02-09, 10:37
lennyw lennyw is offline
Registered User
 
Join Date: Jul 2006
Posts: 3
Thanks for your reply

It worked out perfectly.

I do great with realtime and multithreaded applications but for some reason non-procedural languages like sql and xslt are a blind spot. I really should buy your book

Many thanks. --Lenny
Reply With Quote
  #4 (permalink)  
Old 05-17-09, 15:57
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,053
Quote:
Originally Posted by lennyw
I'm trying to get all records in table ADRS whose zip codes are NOT present in table EXCLUDE's zip code column.
Alternative is to use NOT EXISTS and a subquery:
Code:
SELECT First
     , Last
     , Adr
     , City
     , St
     , Zip
  FROM ADRS 
 WHERE NOT EXISTS (SELECT 1
                     FROM EXCLUDE 
                    WHERE Zip = ADRS.Zip)
This should normally be equally performant as the outer join.
I often perfer it over the join solution because each of the two queries refers to a single table (hence does not need the table prefix for the column names).
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 05-17-09 at 16:07.
Reply With Quote
  #5 (permalink)  
Old 07-01-09, 10:45
Vaibhav Vyas Vaibhav Vyas is offline
Registered User
 
Join Date: Jul 2009
Posts: 17
Smile

SELECT First
, Last
, Adr
, City
, St
, Zip
FROM ADRS
WHERE Zip NOT IN (SELECT EXCLUDE.zip
FROM EXCLUDE
)
Reply With Quote
  #6 (permalink)  
Old 07-02-09, 18:05
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,053
Quote:
Originally Posted by Vaibhav Vyas
SELECT First
, Last
, Adr
, City
, St
, Zip
FROM ADRS
WHERE Zip NOT IN (SELECT EXCLUDE.zip
FROM EXCLUDE
)
Beware that this will return an empty result whenever there is a row in EXCLUDE for which ZIP is NULL.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #7 (permalink)  
Old 07-02-09, 18:09
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,053
Or yet an other way to write it:
Code:
SELECT First
     , Last
     , Adr
     , City
     , St
     , Zip
  FROM ADRS 
 WHERE Zip IN (SELECT Zip FROM adrs
               EXCEPT
               SELECT Zip FROM EXCLUDE)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On