all groups > sql server (alternate) > july 2004 >
You're in the

sql server (alternate)

group:

How do I select all records in one table that have NO related records in another table?


How do I select all records in one table that have NO related records in another table? Robin Tucker
7/27/2004 5:00:20 PM
sql server (alternate):

I can't get my head around this:

I want to select all IDs from table A that do not have a related record in
table B according to some condition:

Table A contains, say, Parents and table B contains Children. I want to
select all Parents that have no children called "Sally" (this is a noddy
example, reminds me of being at Uni again :) ).

Any ideas?


Thanks




Re: How do I select all records in one table that have NO related records in another table? David Portas
7/27/2004 5:17:11 PM
Try one of these:

SELECT A.id
FROM A
WHERE NOT EXISTS
(SELECT *
FROM B
WHERE B.id = A.id
AND B.name = 'Sally')

SELECT A.id
FROM A
LEFT JOIN B
ON A.id = B.id
AND B.name = 'Sally'
WHERE B.id IS NULL

--
David Portas
SQL Server MVP
--

Re: How do I select all records in one table that have NO related records in another table? Robin Tucker
7/28/2004 11:57:50 AM
Ahh, makes sense. Thankyou very much

[quoted text, click to view]

AddThis Social Bookmark Button