Snippet Name: Show Nth highest value Description: Select and display the Nth highest value from a table. Comment: (none) Language: PL/SQL Highlight Mode: PLSQL Last Modified: March 02nd, 2009
Description: Select and display the Nth highest value from a table.
SELECT LEVEL, MAX('col_name') FROM my_table WHERE LEVEL = '&n' CONNECT BY PRIOR ('col_name') > 'col_name') GROUP BY LEVEL; -- Example : -- -- Given a table called emp with the following columns: -- id number -- name varchar2(20) -- sal number -- -- For the second highest salary: SELECT LEVEL, MAX(sal) FROM emp WHERE LEVEL=2 CONNECT BY PRIOR sal > sal GROUP BY LEVEL