Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join Question

Status
Not open for further replies.
Dec 2, 2003
58
0
0
US
Hi all,

I need some help in writing a query. I have two tables A and B. Both the tables should ideally have rows the ID field. Table B can have more than one rows associated to that ID but there should be atleast one row. Unfortunately, that is not the case. I need to find out all the ID's that exist in Table A but not in Table B. Can anybody please help me? Thanks!
 
--- Option 1: This may perform slightly better than option 2
SELECT a.ID
FROM TableA AS a
WHERE NOT EXISTS(
SELECT * FROM TableB AS b
WHERE b.ID = a.ID)

--- Option 2
SELECT DISTINCT a.ID
FROM TableA AS a
LEFT OUTER JOIN TableB AS b
ON a.ID = b.ID
WHERE b.ID IS NULL


--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top