SQL> conn scott/tiger SQL> SELECT empno, ename, sal, RANK() OVER (ORDER BY sal DESC ) AS rk FROM emp; EMPNO ENAME SAL RK --------- ---------- ---------- ---------- 7839 KING 5000 1 7788 SCOTT 3000 2 7902 FORD 3000 2 7566 JONES 2975 4 7698 BLAKE 2850 5 7782 CLARK 2450 6 7499 ALLEN 1600 7 7844 TURNER 1500 8 7934 MILLER 1300 9 7521 WARD 1250 10 7654 MARTIN 1250 10 7876 ADAMS 1100 12 7900 JAMES 950 13 7369 SMITH 800 14
SQL> SELECT deptno, ename, sal, RANK() OVER (PARTITION BY deptno ORDER BY sal DESC ) AS rk FROM emp ; DEPTNO ENAME SAL RK ------- ---------- ---------- --------- 10 KING 5000 1 10 CLARK 2450 2 10 MILLER 1300 3 20 SCOTT 3000 1 20 FORD 3000 1 20 JONES 2975 3 20 ADAMS 1100 4 20 SMITH 800 5 30 BLAKE 2850 1 30 ALLEN 1600 2 30 TURNER 1500 3 30 WARD 1250 4 30 MARTIN 1250 4 30 JAMES 950 6
SQL> SELECT empno, ename, sal, DENSE_RANK() OVER (ORDER BY sal DESC ) AS rk FROM emp; EMPNO ENAME SAL RK --------- ---------- ---------- --------- 7839 KING 5000 1 7788 SCOTT 3000 2 7902 FORD 3000 2 7566 JONES 2975 3 7698 BLAKE 2850 4 7782 CLARK 2450 5 7499 ALLEN 1600 6 7844 TURNER 1500 7 7934 MILLER 1300 8 7521 WARD 1250 9 7654 MARTIN 1250 9 7876 ADAMS 1100 10 7900 JAMES 950 11 7369 SMITH 800 12