SQL query to find Nth highest salary

前端 未结 9 799
清酒与你
清酒与你 2020-12-15 01:52

I am referring to following query to find Nth highest salary of a employee.

select sal from emp t where &n = (select count(sal) from (select distinct sal         


        
相关标签:
9条回答
  • 2020-12-15 02:30

    To get the nth highest salary value just put the value of 'N'.

    Select Min(Salary) From (Select Top N * From Table_Name Order by Salary Desc);
    
    0 讨论(0)
  • 2020-12-15 02:30

    In the database record data entries like

    employ_id    NAME     salary
    101          Henry    24000
    102          Smith    24000
    105          Roy      17000  
    106          Robbin   15000 
    702          Mac      2500
    708          Bill     2100
    709          Kane     2000
    710          Ted      2000
    

    here Some of employees having same salary then how to calculate nth (highest/lowest)salary

    for the calculation of 3rd highest salary

    select * from emloyees where salary in (select salary from (select rownum rank , salary from (select distinct salary from employees order by salary **desc**)) where rank =3;
    

    ans = 15000
    

    similarly to calculate 3rd lowest salary Type same Query with a small change instead of desc type asc

    select * from emloyees where salary in (select salary from (select rownum rank , salary from (select distinct salary from employees order by salary **asc**)) where rank =3;
    

    Hope This will help you

    0 讨论(0)
  • 2020-12-15 02:34
    select sal 
    from (
      select sal, 
             dense_rank() over (order by sal desc) as rnk
    ) t
    where rnk = 5;
    

    Replace where rnk = 5 with whatever "nth" you want.

    0 讨论(0)
  • 2020-12-15 02:34

    There are so many ways to achieve this:-

    1)

     Select Top(1) sal from emp 
        where sal not in (select DISTINCT top(n-1) sal from emp order by sal desc)
    

    2)

    select salary     
              from (
               select salary,
               roe_number() over (order by salary ) as row from emp
              ) emp1
      where row= n;
    
    • This query will not work if multiple rows have the same values one after another.

    3)

    select salary     
                  from (
                   select salary,
                   dense_rank() over (order by salary ) as row from emp
                  ) emp1
          where row= n;
    
    • This will create a unique row number for all unique salary amounts.

    4)

     Select Min(sal) From 
           (Select DISTINCT Top n * From emp Order by sal Desc)as emp1;
    

    5)

       SELECT * FROM emp Emp1
                WHERE (n-1) = (
                                 SELECT COUNT(DISTINCT(Emp2.Sal))
                                 FROM emp Emp2
                                 WHERE Emp2.Sal > Emp1.Sal)
    
    0 讨论(0)
  • 2020-12-15 02:38

    Change nth highest salary value just put the value of 'N'

    SELECT e1.EmployeeName, e1.EmployeeSalary from Employee e1
    where N = (
    select COUNT(e2.EmployeeSalary) from Employee e2 where e2.EmployeeSalary >= e1.EmployeeSalary)
    
    0 讨论(0)
  • 2020-12-15 02:41

    Query:

    select 
        ename  
        ,sal  
        ,dense_rank() over (order by sal desc) ranking  
    from   emp;  
    

    output:

    ENAME   SAL   RANKING
    KING    5000    1   
    FORD    3000    2  
    SCOTT   3000    2  
    JONES   2975    3  
    CLARK   2850    4  
    BLAKE   2850    4  
    ALLEN   1600    5  
    

    Wrap a filter around and pick out the Nth highest salary, say the 4th highest salary.

    Query:

    select *  
    from  
    (  
      select ename  
            ,sal  
            ,dense_rank() over (order by sal desc) ranking  
      from   emp  
    )  
    where ranking = 4 -- Replace 4 with any value of N  
    

    output:

    ENAME  SAL  RANKING
    BLAKE  2850     4  
    CLARK  2850     4  
    
    0 讨论(0)
提交回复
热议问题