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)
 how to compare two tables

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-02-19 : 13:29:19
I have two tables same same format.
How do i list the record which is not exist on next table.

On tbl1
ID
1
2
3
4
5

On tbl2
Id
1
5
6
7

Now i have to list the ID's from tbl2 which is not exist on tbl1

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-19 : 13:33:19
[code]Select ID from Table1
except
Select Id from Table2[/code]
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-02-19 : 13:37:59
thanks sodeep
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-20 : 00:01:55
try this too
select id from table1 where id not in (select id from table2)
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-02-20 : 01:54:39
Also try this

select id from @temp t where not exists (select id from @temp1 where id = t.id)
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-02-20 : 05:56:26
SELECT t.Id FROM Table1 t
LEFT JOIN Table2 t2 ON (t.id = t1.id)
WHERE t2.Id IS NULL

Jai Krishna
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-02-20 : 05:59:53
quote:
Originally posted by bklr

try this too
select id from table1 where id not in (select id from table2)



Replace table1 with table2 and table2 with table1
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-20 : 06:05:07
quote:
Originally posted by raky

quote:
Originally posted by bklr

try this too
select id from table1 where id not in (select id from table2)



Replace table1 with table2 and table2 with table1


hai raky,
see this given query is giving correct result
declare @tbl1 table (id int)
insert into @tbl1 select 1
insert into @tbl1 select 2
insert into @tbl1 select 3
insert into @tbl1 select 4
insert into @tbl1 select 5

declare @tbl2 table (id int)
insert into @tbl2 select 1
insert into @tbl2 select 5
insert into @tbl2 select 6
insert into @tbl2 select 7

Select ID from @tbl1
except
Select Id from @tbl2

select id from @tbl1 where id not in (select id from @tbl2)

see u suggested query
select id from @tbl2 where id not in (select id from @tbl1)
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-20 : 06:19:38
You can have cases when not in doesn't work.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-20 : 07:41:29
quote:
Originally posted by sodeep

You can have cases when not in doesn't work.


Also it wont throw errors in some cases


declare @t1 table(i int)
insert into @t1
select 10
declare @t2 table(j int)
insert into @t2
select 101

--Column i doesnt exist in @t2
select * from @t1 where i not in (select i from @t2)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-20 : 08:08:07
Here is why?
http://weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx
Go to Top of Page
   

- Advertisement -