1

Here is my code to fetch 2nd highest salary in a company...

select salary from org order by salary desc limit(1,1)

above result will be one row ...with highest salary(100000) ,,,now i want to fetch all employes emp_names with the second highest salary.. how to do that?

Friend
  • 1,326
  • 11
  • 38
  • 62

1 Answers1

3

Add DISTINCT to your query (in case multiple people have the same highest salary), and join it like this:

select org.* from org
join (select distinct salary from org order by salary desc limit(1,1)) org_salary
  on org.salary = org_salary.salary

Working sqlfiddle here

PinnyM
  • 35,165
  • 3
  • 73
  • 81