Informatica Questions
Informatica Questions
According to our table, the highest salary earner is King, the president, second highest
earners are Scott and Ford while the third highest salary earner is Jones and so on.
DIFFERENT WAYS TO
FIND NTH HIGHEST
SALARY IN ORACLE
Find Nth Highest Salary Using
DENSE_RANK() Function
Below is the query you can execute to find nth highest salary using dense_rank()
function
Syntax
SELECT *
FROM
FROM emp
Here you can replace n with 1, 2 as first highest, second highest salary respectively.
Example
SELECT *
FROM
FROM emp
WHERE nth_highest_sal = 2;
Output
SCOTT 3000 2
FORD 3000 2
Syntax
SELECT e3.empno empno, e3.ename name, e3.sal salary
FROM (
Here you can replace n with 1, 2 as first highest, second highest salary respectively.
Example
SELECT e3.empno empno, e3.ename name, e3.sal salary
FROM (
WHERE RANK = 2
Output
Syntax
SELECT empno, ename, sal
FROM emp e1
WHERE &n = (SELECT COUNT(DISTINCT(sal)) FROM emp e2 WHERE e2.sal >= e1.sal);
Replace n with value 1,2 to get first highest and second highest salary respectively.
Example
SELECT empno, ename, sal
FROM emp e1
Output
EMPNO ENAME
7788 SCOTT
7902 FORD
FROM (
FROM emp)
Here you can replace n with 1, 2 as first highest, second highest salary respectively.
Example
SELECT empno as emp_number, ename as name, salary
FROM (
FROM emp )
WHERE nth_highest_salary = 2;
Output
EMP_NUMBER NAME
7788 SCOTT
Drawback: The only drawback of this query is employee FORD with same salary will
be regarded as 3rd highest rather than 2nd highest salary as the ranking is given by row
number instead of salary as in DENSE_RANK function.
Find Nth Highest Salary using
LEVEL and CONNECT BY PRIOR
Here is how we can find nth highest salary using Level and Connect by Prior
Syntax
SELECT LEVEL, MAX(sal) salary
FROM emp
GROUP BY LEVEL;
Here you can replace n with 1, 2 as first highest, second highest salary respectively.
Example
SELECT LEVEL, MAX(sal) salary
FROM emp
WHERE LEVEL = 2
GROUP BY LEVEL;
Output
LEVEL SALARY
2 3000
Drawback: The drawback in this query is that you cannot find which employee had
second highest salary.
Hope you like the article and find it useful. We highly appreciate comments and
feedback.