Find max and second max salary for a employee table MySQL

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

    This is awesome Query to find the nth Maximum: For example: -

    1. You want to find salary 8th row Salary, Just Changed the indexed value to 8.

    2. Suppose you have 100 rows with Salary. Now you want to find Max salary for 90th row. Just changed the Indexed Value to 90.

      set @n:=0;
      select * from (select *, (@n:=@n+1) as indexed from employee order by Salary desc)t where t.indexed = 1;
      

提交回复
热议问题