Write a SQL Query to find the second maximum salary without using TOP keyword. Consider the following Employee table
EmpID Salary
1 1000
2 2000
3 5200
4 7855
5 4000
6 6500
7 7000

 Posted by Nagasundar_Tn on 12/1/2012 | Category: Sql Server Interview questions | Views: 2202 | Points: 40

Here the main constraint is we should not use the TOP keyword.

The query is

select MAX(Salary) from tbl_Employee

where Salary <> (
select MAX(Salary) from tbl_Employee)

In sub query I am first getting the Maximum salary of Employee Table. Now 7855 will come. In the outer query I am using Where condition to check the salary NOT EQUAL to 7855 and gettting the maximum value in select query. So now the query will be
select MAX(Salary) from tbl_Employee

where Salary <> 7855

The above query gives 7000 which is the second maximum salary

Asked In: GAVS Technology | Alert Moderator 

Comments or Responses

Login to post response