| Posted by SQL Menace on 07/04/84 11:51 
be carefull with NULLS and IN, if tableB has even 1 NULL for theclient_id column nothing will be returned
 better to add IS NOT NULL
 
 SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
 tableB WHERE client_id IS NOT NULL )
 
 or use NOT EXISTS
 
 SELECT * FROM tableA A WHERE NOT EXISTS  (SELECT * FROM
 tableB WHERE client_id = A.client_id )
 
 
 Denis the SQL Menace
 http://sqlservercode.blogspot.com/
 
 
 Jason wrote:
 > Try one of these:
 >
 > SELECT * FROM tableA WHERE client_id NOT IN (SELECT client_id FROM
 > tableB)
 >
 > SELECT a.*
 > FROM tableA a
 > LEFT JOIN tableB b ON a.client_id = b.client_id
 > WHERE b.client_id IS NULL
 >
 > you should see better performance on the second one if you have a lot
 > of rows in your tables.
 >
 > Jason
 [Back to original message] |