Select Top 1 * From (Select Top 3 * From EmployeeTable Order By Salary)
Order By Salary Desc
Another way to do this would be to use the rank functionality (which will bring back all employees that have the 3rd highest salary, where employees might have the same salary):
select employee, salary
from
(select employee, salary,
rank () over
(order by salary desc) as salary_rank
<b>from EmployeeTable</b> )
where salary_rank = 3
--------------------------------
This need to be done in different ways depending on the database.
To get the TOP n rows in Oracle you could use the rownum pseudocolumn with a subquery. Something like this (in this case n = 10):
<pre>SELECT *
FROM (SELECT * FROM my_table ORDER BY colname)
WHERE rownum < 10;</pre>
So, the equivalent to the 'select top 1 from ... top 3...' would be something like this:
<pre>SELECT * FROM
(SELECT *
FROM (SELECT * FROM EmployeeTable ORDER BY salary)
WHERE ROWNUM <= 3
ORDER BY salary DESC)
WHERE rownum = 1;</pre>
Or you could use the analytic functions RANK and DENSE_RANK as mentioned above.
You might want to take a look at <a href="http://itknowledgeexchange.techtarget.com/itanswers/finding-the-3rd-highest-in-the-table-using-sql/">this similar question/discussion</a>
Last Wiki Answer Submitted: January 26, 2009 3:55 pm by Darryn765 pts.
All Answer Wiki Contributors: Darryn765 pts. ,
Dalvarez30 pts.
If you live outside the United States, by submitting your email address you consent to having your personal data transferred to and processed in the United States.