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
 Help with query needed

Author  Topic 

barnabeck
Posting Yak Master

236 Posts

Posted - 2011-10-28 : 04:36:55
Table1
Name:
-----
Paul
Ringo
John
Paul
George
Ringo

Table2
Name
------
Steve
George
Peter
Paul
John
George


I want all those records from Table1 who's names are not in the List of Table2; in this case the wanted result is

Ringo
Ringo

how can I achieve this?
Martin

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-10-28 : 04:39:10
select name from table1 as t1
where not exists(select * from table2 as t2 where t1.name=t2.name)



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-10-28 : 04:42:03
select name from table1
except
select name from table2



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2011-10-28 : 04:42:51
Now that was quick... thank you!!!
Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2011-10-28 : 06:47:22
@SwePeso: This query just returns one "Ringo" - I need all the records from Table1, that do not exist in Table2

@Webfred: I can't adapt the syntax to my query as table1 is a inner join relationship... :( Could save it as a new table and refer to it that way, but I rather learn how to do this in one expression.

table1:
select * from table1 inner join table3 on table1.xx = table3.xx

So in application to your query I try:

select * from table1 inner join table3 on table1.xx = table3.xx as t1
where not exists(select * from table2 as t2 where t1.name=t2.name)

Incorrect syntax near the keyword 'as'

hmmm... Martin
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-28 : 06:56:20
you can use like


select t1.*
from
(
select * from table1 inner join table3 on table1.xx = table3.xx
) as t1
where not exists(select * from table2 as t2 where t1.name=t2.name)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

barnabeck
Posting Yak Master

236 Posts

Posted - 2011-10-28 : 07:19:24
Nice! Now it works!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-28 : 07:22:52
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-10-28 : 10:07:58
SELECT * FROM table1 LEFT JOIN table2 ON table1.Name = table2.Name WHERE table2.Name IS NULL

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-28 : 11:03:09
quote:
Originally posted by X002548

SELECT * FROM table1 LEFT JOIN table2 ON table1.Name = table2.Name WHERE table2.Name IS NULL

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/





what OP told as table1 is actually a query by itself

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -