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 > Where Not Exists!?!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-29-04, 06:25
steve_o steve_o is offline
Registered User
 
Join Date: Apr 2004
Posts: 43
Question Where Not Exists!?!

I am trying to find all items in one table that do not exist in another table.

The following query of my config table returns one row (which is '350'):
SELECT config.pos from config

The following query of my gsl table returns 47 rows (one of which is '350'):
SELECT gsl.pos from gsl

I do not understand why the follwing query returns no rows, when I would like it to return all rows (except for the one that is in the config table):
SELECT gsl.pos from gsl
WHERE NOT EXISTS (SELECT config.pos from config)

Any ideas would be much appreciated..
Reply With Quote
  #2 (permalink)  
Old 06-29-04, 06:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
it's not working because the subquery returns all the rows in the config table and therefore the outer query doesn't return anything

there are two ways to do what you want
Code:
select gsl.pos 
  from gsl
 where not exists 
     ( select pos 
         from config
        where pos = gsl.pos )
        
select gsl.pos 
  from gsl
left outer
  join config
    on gsl.pos 
     = config.pos
 where config.pos is null
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-29-04, 07:00
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Or if your DBMS supports it:

SELECT gsl.pos from gsl
MINUS
SELECT config.pos from config

(Sometimes it is EXCEPT rather than MINUS).
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #4 (permalink)  
Old 06-29-04, 07:26
steve_o steve_o is offline
Registered User
 
Join Date: Apr 2004
Posts: 43
Thanks both very much for your help!

All solutions work great...

Cheers,
Steve.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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