Find max and second max salary for a employee table MySQL

前端 未结 30 1914
忘掉有多难
忘掉有多难 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: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
    

提交回复
热议问题