Need help with Oracle query

30 pts.
Tags:
Oracle development
Oracle queries
Query optimization
how to display the third highest salary from an employees table thanks in advance
ASKED: January 24, 2009  6:46 AM
UPDATED: January 26, 2009  3:55 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

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>

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following