Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Assume I have two tables:COUNTRY CONTINENT_ASSOC---------------- -------------------------------USA 1 CANADAMEXICO 1 USANow, 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 @countryselect 'USA' union allselect 'Mexico'declare @continent_assoc table(continent_assoc varchar(20))insert @continent_assocselect 'CANADA' union allselect 'USA'
Query-----
select * from @country a where not exists (select * from @continent_assoc b where b.continent_assoc = a.country)
bklr
Master Smack Fu Yak Hacker
1693 Posts
Posted - 2009-08-21 : 00:11:43
do u want like thisSELECT a.* FROM @country a LEFT JOIN @continent_assoc b ON b.continent_assoc = a.countryWHERE b.continent_assoc IS NULLSELECT * FROM @country a WHERE country NOT IN (SELECT continent_assoc FROM @continent_assoc)