SQL query to find third highest salary in company

Viewed 320034

I need to write a query that will return the third highest salaried employee in the company.

I was trying to accomplish this with subqueries, but could not get the answer. My attempts are below:

select Max(salary)
from employees
where Salary not in

 (select Max(salary)
from employees
where Salary not in 

(select Max(salary)
from employees));

My thought was that I could use 2 subqueries to elimitate the first and second highest salaries. Then I could simply select the MAX() salary that is remaining. Is this a good option, or is there a better way to achieve this?

32 Answers

You may use this for all employee with 3rd highest salary:

SELECT * FROM `employee` WHERE salary = (
   SELECT DISTINCT(`salary`) FROM `employee` ORDER BY `salary` DESC LIMIT 1 OFFSET 2
);
SELECT MAX(salary) FROM employees GROUP BY salary ORDER BY salary DESC LIMIT 1 OFFSET 2;

You can use nested query to get that, like below one is explained for the third max salary. Every nested salary is giving you the highest one with the filtered where result and at the end it will return you exact 3rd highest salary irrespective of number of records for the same salary.

select * from users where salary < (select max(salary) from users where salary < (select max(salary) from users))  order by salary desc limit 1

Below query will give accurate answer. Follow and give me comments:

select top 1 salary from (
select DISTINCT  top 3 salary from Table(table name) order by salary  ) as comp
order by personid salary 

you can get any order for salary with that:

select * from 
(
select salary,row_Number() over (order by salary DESC ) RN 
FROM employees
)s
where RN = 3

-- put RN equal to any number of orders. --for your question put 3

You can find Nth highest salary by making use of just one single query which is very simple to understand:-

select salary from employees e1 where N-1=(select count(distinct salary) from employees e2 where e2.salary>e1.salary);

Here Replace "N" with number(1,2,3,4,5...).This query work properly even when where salaries are duplicate. The simple idea behind this query is that the inner subquery count how many salaries are greater then (N-1). When we get the count then the cursor will point to that row which is N and it simply returns the salary present in that row.

SELECT salary FROM employees e1
       WHERE N-1 = (SELECT COUNT(DISTINCT salary) FROM employees e2
                    WHERE e2.salary > e1.salary)

Here, I have solved it with a correlated nested query. It is a generalized Query so if you want to print 4th, 5th, or any number of highest salary it will work perfectly even if there are any duplicate salaries.

So, what you have to do is simply change the N value here. So, in your case, it will be,

SELECT salary FROM employees e1
       WHERE 3-1 = (SELECT COUNT(DISTINCT salary) FROM employees e2
                    WHERE e2.salary > e1.salary)

WITH CTE AS ( SELECT Salary, RN = ROW_NUMBER() OVER (ORDER BY Salary DESC) FROM Employee ) SELECT salary FROM CTE WHERE RN = 3

for oracle it goes like this:

select salary from employee where rownnum<=3 order by salary desc
minus
select salary from employee where rownnum<=2 order by salary desc;

The SQL-Server implementation of this will be:

SELECT SALARY FROM EMPLOYEES OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY

This is a MYSQL query.

Explanation: The subquery returns top 3 salaries. From the returned result, we select the minimum salary, which is the 3rd highest salary.

SELECT MIN(Salary)
FROM (
    SELECT Salary
    FROM Employees
    ORDER BY Salary DESC
    LIMIT 3
) AS TopThreeSalary;

in Sql Query you can get nth highest salary

select * from( select empname, sal, dense_rank() over(order by sal desc)r from Employee) where r=&n;

To find to the 2nd highest sal set n = 2

To find 3rd highest sal set n = 3 and so on.

This works fine with Oracle db.

select SAL from ( SELECT DISTINCT SAL FROM EMP ORDER BY SAL DESC FETCH FIRST 3 ROWS ONLY ) ORDER BY SAL  ASC FETCH FIRST 1 ROWS ONLY
SELECT * 
FROM maintable_B7E8K
order by Salary
desc limit 1 offset 2;

--Oracle SQL with temp as ( select distinct salary from HR.EMPLOYEES order by SALARY desc ) select min(temp.salary) from temp where rownum <= 3;

SELECT * FROM(

SELECT salary, DENSE_RANK()

OVER(ORDER BY salary DESC)r FROM Employee)

WHERE r=&n;

To find the 3rd highest salary set n = 3

Related