Find Second Highest Salary From Table In SQL

I have given a name to my blog as 'Find Second Highest Salary From Table In SQL' because any interview this question is always asked, also according to my personal experience I have faced many interview and the same question is asked in my all interviews so to help freshers i have decided to write this blog,so let us start..
Create a one table named employee as follows


Now insert the some record into the table that shown in below and
selectfrom employee
The output will be look like as follows..


Now let write a query to retrieve second highest salary from employee table
select MAX (salary) as Salary from employee
where salary < 
(select MAX (salary) as Salary from employee)
Run the above query the output will be as follows...

From the above example its clear that ,the above query displays the second highest salary from table,in the above query the nested query is executed first which retrieves the highest salary and because of less than condition in where clause the first query  compares with second query and retrieves less than highest salary that is second.
I hope this article is useful for all readers , If you have any suggestion regarding this article then please contact me.

Post a comment

Protected by Copyscape Online Copyright Protection
Copyright © Compilemode