Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Items Not Joined

Author  Topic 

srolfe
Starting Member

1 Post

Posted - 2009-08-20 : 10:52:49
Assume I have two tables:


COUNTRY CONTINENT_ASSOC
---------------- -------------------------------
USA 1 CANADA
MEXICO 1 USA


Now, I want to write a query to identify that mexico is not in the continent_assoc table, but ignore the USA which is accociated (so the query would return only mexico). How would one do that?

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-08-20 : 10:56:48
Maybe this?

Sample Data

declare @country table (country varchar(20))
insert @country
select 'USA' union all
select 'Mexico'

declare @continent_assoc table(continent_assoc varchar(20))
insert @continent_assoc
select 'CANADA' union all
select 'USA'


Query
-----
select * from @country a where not exists (select * from @continent_assoc b where b.continent_assoc = a.country)
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-08-21 : 00:11:43
do u want like this
SELECT a.* FROM @country a LEFT JOIN @continent_assoc b ON b.continent_assoc = a.country
WHERE b.continent_assoc IS NULL

SELECT * FROM @country a WHERE country NOT IN (SELECT continent_assoc FROM @continent_assoc)
Go to Top of Page
   

- Advertisement -