26.4.09

Self Join + Case statement in SQL Server

Self Join:

Joining the table itself is referred to as self join.

Case statement:

Case statement is used to check the condition

-- Creating tables for our sample
drop table venkat
drop table original
create table venkat(id int)
insert into venkat values(10)
insert into venkat values(11)
insert into venkat values(12)
insert into venkat values(13)
insert into venkat values(14)
----------------------------------------------------
create table Original(id int)
insert into Original values(1)
insert into Original values(2)
insert into Original values(3)
----------------------------------------------------
select * from original
select * from venkat

-- How to use case statement in SQL Server
select a.id from venkat a inner join
(select
case id
when 1 then 10
when 2 then 11
when 3 then 12
end as id
from original)t on a.id=
t.id

--- Self join with the same table.
select a.id from venkat a inner join
(select id from venkat)t on a.id=
t.id

Happy Learning!!!
Thanks and Regards,

Venkatesan Prabu .J

No comments:

Post a Comment