Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find n'th highest value of a column?

Tags:

mysql

Is there a command akin to:

  • 2nd highest salary from tbl_salary or

  • 4th highest salary from tbl_salary ?

I've seen:

select salary
from tbl_salary t
where &n = (
    select count(salary) 
    from(
        select distinct salary
        from tbl_salary
    )where t.salary<=salary
);

How does this it works?

Are there other simple ways to get result?

like image 796
diEcho Avatar asked Feb 24 '10 06:02

diEcho


People also ask

How do you find the nth highest number?

Here is a way to do this task using dense_rank() function. Query : select * from( select ename, sal, dense_rank() over(order by sal desc)r from Employee) where r=&n; To find to the 2nd highest sal set n = 2 To find 3rd highest sal set n = 3 and so on.

How do you find the nth highest value in a column in SQL?

select column_name from table_name order by column_name desc limit n-1,1; where n = 1, 2, 3,.... nth max value.

How do you find the nth largest number in Excel?

The LARGE function is an easy way to get the nth largest value in a range: = LARGE ( range , 1 ) // 1st largest = LARGE ( range , 2 ) // 2nd largest = LARGE ( range , 3 ) // 3rd largest In this example, we can use the LARGE function to get a highest...


1 Answers

select * from employee order by salary desc limit 1,1

Description : limit x,y

  • x: The row offset from which you want to start displaying records. For nth record it will be n-1.
  • y: The number of records you want to display. (Always 1 in this case)
like image 109
Nikhil Navlakha Avatar answered Oct 05 '22 10:10

Nikhil Navlakha