youv youv - 1 year ago 37
MySQL Question

Select entire row as well as third highest salary

I need to fetch all row details of user with third highest salary.

My query

select min(salary)as minsalary from (select * from userdetails
order by salary desc limit 3) as details

Current output :-

[0] => stdClass Object
[minsalary] => 300


i need to fetch the full row details too along with this array as single array output. How can i alter my query.

Please help.

Answer Source

Try this:-

 SELECT * FROM userdetails ORDER BY salary DESC limit 2,1;

Another way to find nth Highest salary could be:-

SELECT * FROM userdetails usr1
WHERE (n-1) = ( 
    SELECT COUNT(DISTINCT(usr2.salary )) 
    FROM  userdetails usr2 
    WHERE usr2.salary > usr1.salary )