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 2005 Forums
 Transact-SQL (2005)
 does not contain in other table... help!

Author  Topic 

SteveReed
Starting Member

2 Posts

Posted - 2008-03-06 : 14:53:47
I want to write SQL statement that will pull out the records from table1 that table2 does not contain.

something like

Select table1.StudentID from table1, table2
WHERE table1.StudentID 'does not contain' table2.StudentID

but what's the approprate way of writing that statement?

Please help!

Thank you
Steve Reed

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2008-03-06 : 14:59:38
select table1.StudendID
from table1
left outer join table2
on table1.StudentID = table2.StudentID
where table2.StudentID is NULL

=======================================
Do something for somebody every day for which you do not get paid. -Albert Schweitzer, philosopher, physician, musician, Nobel laureate (1875-1965)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-03-06 : 15:01:24
SELECT ...
FROM Table1 t1
WHERE NOT EXISTS (SELECT * FROM Table2 t2 WHERE t1.StudentID = t2.StudentID)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

SteveReed
Starting Member

2 Posts

Posted - 2008-03-06 : 15:02:22
That works!

Thank you so much
Steve Reed
Go to Top of Page
   

- Advertisement -