'3rd Highest Salary in the Employees Table [closed]
Is it the right query to return the 3rd Highest Salary in the Employees Table?
SELECT *
FROM employees
WHERE emp_no in (SELECT emp_no FROM employees ORDER BY salary DESC FETCH FIRST 3 ROWS ONLY)
ORDER BY salary ASC
FETCH FIRST 1 ROWS ONLY
Solution 1:[1]
I would use a window function in order to be able to deal with duplicate values:
select *
from (
select *,
dense_rank() over (order by salary desc) as rnk
from employees
) t
where rnk = 3;
Solution 2:[2]
SELECT * FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 2;
Query orders employees by salary and returns one result which from third row in result set. I think this will work for MySQL and PostgreSQL.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | a_horse_with_no_name |
| Solution 2 | Damini Suthar |
