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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 joins

Author  Topic 

Mike_h
Starting Member

7 Posts

Posted - 2007-10-17 : 09:04:52
Hi.. I was wondering how to join two tables together but only return the values that are in one table and not the other with the select statement

E.g

t1 t2
1 1
2 2
3
4

I would like the output of my query to output only 3 and 4 they are present in t1 but not t2

Is there a some sort of join that allows me to do this?

Kristen
Test

22859 Posts

Posted - 2007-10-17 : 09:09:21
See: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=91107
Go to Top of Page

Mike_h
Starting Member

7 Posts

Posted - 2007-10-17 : 09:20:50
select t1.a from t1 left outer join t2
on t1.a = t2.a

would give me all four records.

An inner join would give me records 1 and 2
However i need to get records 3 and 4 returned as they are not present in t2?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-17 : 09:46:32
[code]select t1.a
from t1 left outer join t2
on t1.a = t2.a
where t2.a is null[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-17 : 12:55:12
Ah, sorry. You are in need of a NOT EXISTS. Which you can do in the WHERE clause; or you can use an OUTER JOIN and test for NULL as khtan has shown.

Kristen
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-17 : 12:56:00
An example might help!

select t1.a
from t1
WHERE NOT EXISTS (SELECT * FROM t2 WHERE t1.a = t2.a)

Kristen
Go to Top of Page
   

- Advertisement -