Find nth largest salary from employees table?

Last updated 5 years, 6 months ago | 1736 views 75     5

Tags:- SQL MySQL

By using following query we can find out the nth largest salary from employees table.

SELECT salary FROM `employees` ORDER BY salary DESC LIMIT 1 OFFSET n-1

In the above query n is replaced by any number where n >= 1. Suppose you want to find fifth highest salary then you have to use offset = 5-1 i.e.: 4

The query will be:

SELECT salary FROM `employees` ORDER BY salary DESC LIMIT 1 OFFSET 4