PayPal Interview Question | Highest Salary

Question

Write a SQL query to find the 5th highest employee salary from an Employee Table. Explain your answer.

(Note: You may assume that there are enough records in the Employee Table)

in progress 0
Dhruv2301 4 years 2 Answers 543 views Great Grand Master 0

Answers ( 2 )

  1. select salary from
    (select *, dense_rank over(order by salary desc) as sal) from employee as e
    where e.sal=5;

    Dense rank will assign ranks according to the descending order of the salary. If the salary of two employee are same, it will give the same rank and will not skip the next number.

  2. Select first_name, salary, s.salary_rank from (
    select *, dense_rank() over(order by salary) as salary_rank from salary) s
    where s.salary_rank = 5

    Select first_name, salary from salary
    order by salary desc
    limit 1 offset 4;

    Using window function dense_rank enables us to display the rank as well in the output.

Leave an answer

Browse
Browse