BCG Interview Question | Salary

Question

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

(Note: You may assume that there are at least 15 records in the Employee Table)

in progress 0
Dhruv2301 55 years 3 Answers 1272 views Great Grand Master 0

Answers ( 3 )

  1. select * from (select id, name ,salary, dense_rank() over (order by salary desc) as s from Employee) as e
    where e.s=10;

  2. /*Using Common Table Expression*/ /* n=10 */
    WITH Result as (Select Salary, Dense_Rank OVER (ORDER BY salary desc) as Dense_Rank from Employees)
    SELECT salary
    from Result
    WHERE Dense_Rank =n;

    /*Using LIMIT and OFFSET*/
    Select *from Employees
    Where Salary =(Select Distinct(Salary) from Employees Order by salary LIMIT n-1,1);

  3. Select * from (
    select ename,sal,dense_rank() OVER(order by sal desc)r from employee)
    where r = 10

    Explanation:
    we will use the window function dense_rank() to rank according to descending
    salaries and filter the rows which are getting a rank of 10

Leave an answer

Browse
Browse