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 2008 Forums
 Transact-SQL (2008)
 select statement help

Author  Topic 

gadedineshreddy
Starting Member

3 Posts

Posted - 2011-03-21 : 15:17:41
how to query records in col-1 from table-1 where those records doesnot exist in col-1 in table-2 ?

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-21 : 15:24:54
Homework?

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

gadedineshreddy
Starting Member

3 Posts

Posted - 2011-03-21 : 15:41:57
I have tried this but doesn't work. Need to change the and part in the where clause:

select [USER].USER_ID, USER_NM,[USER].LAST_UPDT_BY_NM ,[USER].LAST_UPDT_TS from dbo.[USER]
join USER_ROLE on [USER].USER_ID=USER_ROLE.USER_ID
where ACTIVE_IN='1' and ( [USER].USER_ID != [USER_ROLE].USER_ID)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-21 : 15:52:45
You can use NOT EXISTS or LEFT JOIN/IS NULL for this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

latch
Yak Posting Veteran

62 Posts

Posted - 2011-03-21 : 15:56:51
Hi,

Might these examples helps you.

SELECT T1.col1
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON Tl.col1 = T2.col1
WHERE T1.col1 <> T2.Col1

SELECT T1.col1
FROM TABLE1 T1
WHERE T1.col1 NOT IN (SELECT T2.Col1
FROM TABLE2 T2
GROUP BY T2.Col1)


SELECT T1.Col1
FROM TABLE1 T1
WHERE NOT EXISTS(SELECT T2.col1
FROM TABLE2 T2)

Thanks,
latch
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-21 : 15:58:26
Don't use NOT IN.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

gadedineshreddy
Starting Member

3 Posts

Posted - 2011-03-21 : 16:12:01
Thank You !!
Go to Top of Page
   

- Advertisement -