How to find the employee with the second highest salary?

前端 未结 5 2003
生来不讨喜
生来不讨喜 2020-12-02 01:10

Is there any predefined function or method available to get the second highest salary from an employee table?

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 01:24

    In Oracle you would use a query such as this one to return paged results (rows M to N):

    SELECT NAME, salary
      FROM (SELECT NAME, salary, ROWNUM r 
              FROM (SELECT NAME, salary 
                      FROM employee 
                     ORDER BY salary DESC
                    )
             WHERE ROWNUM <= :N
            )
     WHERE r >= :M
    

    Alternatively, you can use analytics:

    SELECT NAME, salary
      FROM (SELECT NAME, salary, row_number() over (ORDER BY salary DESC) n 
              FROM employee)
     WHERE n BETWEEN :M AND :N
    

提交回复
热议问题