Find max and second max salary for a employee table MySQL

前端 未结 30 1897
忘掉有多难
忘掉有多难 2020-12-12 21:19

Suppose that you are given the following simple database table called Employee that has 2 columns named Employee ID and Salary:

  Employee
  Employee ID    S         


        
相关标签:
30条回答
  • 2020-12-12 21:37

    i think that the simple way in oracle is this:

    SELECT Salary FROM
    (SELECT DISTINCT Salary FROM Employee ORDER BY Salary desc)
    WHERE ROWNUM <= 2;
    
    0 讨论(0)
  • 2020-12-12 21:37

    Find Max salary of an employee

    SELECT MAX(Salary) FROM Employee
    

    Find Second Highest Salary

    SELECT MAX(Salary) FROM Employee 
    Where Salary Not In (Select MAX(Salary) FROM Employee)
    

    OR

    SELECT  MAX(Salary) FROM Employee
    WHERE Salary <> (SELECT MAX(Salary) FROM Employee )
    
    0 讨论(0)
  • 2020-12-12 21:37
    Select Distinct sal From emp Order By sal Desc Limit 1,1;
    

    It will take all Distinct sal. And Limit 1,1 means: leaves top one record and print 1 record.

    0 讨论(0)
  • 2020-12-12 21:39

    Simplest way to fetch second max salary & nth salary

    select 
     DISTINCT(salary) 
    from employee 
     order by salary desc 
    limit 1,1
    

    Note:

    limit 0,1  - Top max salary
    
    limit 1,1  - Second max salary
    
    limit 2,1  - Third max salary
    
    limit 3,1  - Fourth max salary
    
    0 讨论(0)
  • 2020-12-12 21:39

    Try below Query, was working for me to find Nth highest number salary. Just replace your number with nth_No

    Select DISTINCT TOP 1 salary
    from 
    (Select DISTINCT TOP *nth_No* salary
    from Employee
    ORDER BY Salary DESC)
    Result
    ORDER BY Salary
    
    0 讨论(0)
  • 2020-12-12 21:40

    This will work To find the nth maximum number

    SELECT 
        TOP 1 * from (SELECT TOP  nth_largest_no * FROM Products Order by price desc) ORDER BY price asc;
    

    For Fifth Largest number

    SELECT 
      TOP 1 *  from (SELECT TOP  5 * FROM Products Order by price desc) ORDER BY price asc;
    
    0 讨论(0)
提交回复
热议问题