Select second higest value from a database table.

By | November 20, 2014

Example:

my table ‘salary_table’ with name and salary fields

id name salary
1 vasanthan 300
2 sunil 300
3 nasar 200
4 raja 100
5 Raina 400
6 Haier 600
7 shaji 400

for getting second highest salary use the following queries

SELECT MAX( salary ) FROM salary_table WHERE salary NOT IN (SELECT MAX( salary ) FROM salary_table)

// output : 400

select distinct salary from salary_table order by salary desc limit 1,1

// output : 400

for getting third higest salary you can use the following query

select distinct salary from salary_table order by salary desc limit 2,1

// output : 300