name salary
----- -----
mohan 500
ram 1000
dinesh 5000
hareesh 6000
mallu 7500
manju 7500
praveen 10000
hari 10000
How would I find the nth-highest salary from the aforementioned table using Oracle?
name salary
----- -----
mohan 500
ram 1000
dinesh 5000
hareesh 6000
mallu 7500
manju 7500
praveen 10000
hari 10000
How would I find the nth-highest salary from the aforementioned table using Oracle?
Refer following query for getting nth highest salary. By this way you get nth highest salary. If you want get nth lowest salary only you need to replace DESC by ASC in the query.
You've tagged your question Oracle so you could just use the
NTH_VALUE()
function... it's an analytic function unfortunately but your query would be simplified to:SQL Fiddle
From 12c Oracle finally catches up with the rest of the world and includes OFFSET so you could use this instead:
you can use something like this.. this is what i have tested and then pasted here
in place of 'tblname' give your table name and then in place nth give your desired nth highest salary that you want
you can see in the screen shot that it is working.
pass your salary number in place of "nth"
We can do this by correlated subquery.
For further knowledge please check this link.. Correlated Subquery with example
You can find plenty of stuff on google