Wednesday, August 18, 2010

How to get second highest record from table?


Generally a daft question asked by a nincompoop to other dunce who think that this is a lallapalooza of ones's knowledge of data base queries ?(none of wasn't me :) )
Don't throw conniptions at him because it is very difficult , I think you should realize that how easy it is and it is a bugaboo to just scare away you .

guess which question ;) ?

How to get second highest record from a table in MY SQL ?
or generic how to get Nth highest record in MY SQL ?

Consider following table















How to get second highest record from a table in MY SQL ?

SELECT name , salary FROM employee WHERE salary < (SELECT max( salary) FROM employee ) order by salary DESC LIMIT 1 OR using generic query N =2 , Offset =1

SELECT name, salary FROM employee order by salary DESC LIMIT 1,1

To get Generic Nth Highest record

SELECT name, salary FROM employee order by salary DESC LIMIT (N-1),1

LIMIT X, Y --> suggest that X is row from which onward to start and Y is number of rows or offset

example of limit clause
SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

No comments: