How can I select the record with the 2nd highest salary in database Oracle?

后端 未结 18 1269
孤城傲影
孤城傲影 2020-12-30 17:27

Suppose I have a table employee with id, user_name, salary. How can I select the record with the 2nd highest salary in Oracle?

I googled it, find this solution, is t

相关标签:
18条回答
  • 2020-12-30 17:35

    If you're using Oracle 8+, you can use the RANK() or DENSE_RANK() functions like so

    SELECT *
    FROM (
      SELECT some_column, 
             rank() over (order by your_sort_column desc) as row_rank
    ) t
    WHERE row_rank = 2;
    
    0 讨论(0)
  • 2020-12-30 17:35

    You should use something like this:

    SELECT *
    FROM (select salary2.*, rownum rnum from
             (select * from salary ORDER BY salary_amount DESC) salary2
      where rownum <= 2 )
    WHERE rnum >= 2;
    
    0 讨论(0)
  • 2020-12-30 17:41
    select max(Salary) from EmployeeTest where Salary < ( select max(Salary) from EmployeeTest ) ;
    

    this will work for all DBs.

    0 讨论(0)
  • 2020-12-30 17:42

    I believe this will accomplish the same result, without a subquery or a ranking function:

    SELECT *
    FROM emp
    ORDER BY sal DESC
    LIMIT 1
    OFFSET 2
    
    0 讨论(0)
  • You can use two max function. Let's say get data of userid=10 and its 2nd highest salary from SALARY_TBL.

    select max(salary) from SALARY_TBL
    where 
    userid=10
    salary <> (select max(salary) from SALARY_TBL where userid=10)
    
    0 讨论(0)
  • 2020-12-30 17:49
    select salary from EmployeeDetails order by salary desc limit 1 offset (n-1).
    

    If you want to find 2nd highest than replace n with that 2.

    0 讨论(0)
提交回复
热议问题