Write a query for second highest salary using SQL Server?

Second Highest Salary

We can calculate send highest salary using lot of ways. Here i am giving 3 example ways for that.

Before going to use below script please go run this SQL scripts for creation of tables with data.

Way 1
select * from employeemaster a where 2=(select distinct(COUNT(*)) from employeemaster b where a.sal<=b.sal)


Way 2
Select * from ( select  Row_number() over (  order by sal desc ) as Row_INDEX , * From employeemaster ) as Temp where Row_INDEX = 2


Way 3
Select top 1* from employeemaster e where e.Sal <(Select Max(Sal) from 
employeemaster)  order by Sal DESC

If any other ways, blog visitors can write query in the comments.

Output of above 3 scripts will be.
Write a query for second highest salary using SQL server
Write a query for second highest salary using SQL server

For more interview Questions, please go through SQL Server Interview Questions.


Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar