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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

LIMIT is not supported inside an IN () subquery. This will error.
That answer doesn't use an IN() subquery, it uses a joined subquery where LIMIT does work.
just curious to know is there any other way..?
@user - sure, you could do it with two queries instead (one to get the salary, another to get all the employees with that salary) but not sure why you would want to.
k in case if i use two queries.. how to give where condition?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.