Interesting Query

0

Hi Guys! Today one of trainee in my team asked me one strange question, he shows a table and data –

querysql

Then he asked me to get first 7th id row then 15th id row then 10th id row after this other rows-

I tried few queries, Hope this will help you too-

Solution1 :

select * from sales where id=7
union all
select * from sales where id=15 –and id not in (7)
union all
select * from sales where id=10 –and id not in (7,15)
union all
select * from sales where id not in (7,10,15)

querysql1

Solution2:

Select * from sales
Order by Case when id=7 then 1 when id=15 then 2 when id=10 then 3 else 4 End

querysql2

I am Microsoft Certified Professional for querying & Administering Sql Server. I believe “The greatest barrier to success is the fear of failure.”
I am also available at http://dbavimal.blogspot.com

%d bloggers like this: