SQL Server FAQ

Here are some mostly useful FAQs on SQL Server
==============================
1. For finding the Nth salary
 
Method 1:
declare @sal int
set @sal=(select top 1 sal from emp where empid not in (select top 0 empid from emp order by
sal desc) order by sal desc)
print @sal
select * from emp where sal=@sal
         This will give u the top most salary
Method 2:
 select top 1 sal  from (select top 1 * from emp order by sal desc) sal
order by sal
Method 3: [Better One]
Select * From Emp X Where 5 =
(
Select Count(Distinct Sal) From Emp Where sal >=X.sal
)
 
Advertisements

One Comment on “SQL Server FAQ”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s