30 pts.
 Need help with Oracle query
how to display the third highest salary from an employees table thanks in advance

Software/Hardware used:
ASKED: January 24, 2009  6:46 AM
UPDATED: January 26, 2009  3:55 PM

Answer Wiki:
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  Darryn   765 pts.
All Answer Wiki Contributors:  Darryn   765 pts. , Dalvarez   30 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _