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 > Database Server Software > DB2 > check records that does not exists in any of the two tables.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-22-11, 12:06
angeldust03 angeldust03 is offline
Registered User
 
Join Date: Feb 2010
Posts: 8
Exclamation check records that does not exists in any of the two tables.

Hi,

I would just like to know what sql statement should I use to check if records Table A does not exists in any of the tables , Table B and Table C.
My idea is to use this

Select books
from table a
where a.books not exists = ((select b.books
from table b
where b.books = a.books)
or (select c.books
from table c
where c.books = a.books))

Thanks,
Reply With Quote
  #2 (permalink)  
Old 09-22-11, 15:13
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
It depends ...
if tableB and tableC are small, try:

SELECT books
FROM tableA a
WHERE a.books NOT IN (SELECT books FROM tableB)
OR a.books NOT IN (SELECT books FROM tableC)


if tableB and tableC are medium or large, try:
SELECT books
FROM tableA a
WHERE NOT EXISTS (SELECT * FROM tableB B WHERE a.books = b.books)
OR NOT EXISTS (SELECT * FROM tableC C WHERE a.books = c.books)


you might also use a JOIN:

SELECT a.books
FROM tableA a
LEFT JOIN tableb B
ON a.books = b.books
LEFT JOIN tablec C
ON a.books = c.books
WHERE b.books IS NULL OR c.books IS NULL

Last edited by umayer; 09-22-11 at 15:18.
Reply With Quote
  #3 (permalink)  
Old 09-22-11, 16:52
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Don't forget the UNION ALL option.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
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