QUERY DOUBT

greenspun.com : LUSENET : DBAzine : One Thread

IF I HAVE A NUMBER OF RECORDS SAY A COMPANY'S SALARY DETAILS.HOW CAN I RETRIEVE SECOND HIGHEST SALARY BY MEANS OF SQL QUERY

-- ILAYARAJA (i_ilayaraja@yahoo.com), May 22, 2004

Answers

The following query selects the second maximum salary from the employee table. The number 2 can be replaced with n to get the 'n'th maximum salary.

SELECT MAX(SALARY) FROM EMPLOYEE A WHERE 2 > (SELECT COUNT(DISTINCT(SALARY)) FROM EMPLOYEE B WHERE A.SALARY > B.SALARY) ;

Hope this helps.

-- Krishna Kumar (krishmaths@gmail.com), July 28, 2004.


Moderation questions? read the FAQ