0% found this document useful (0 votes)
78 views7 pages

Rank Functions:: Select Ename, Job, Sal, Row - Number Over (Order by Sal Desc) RN From Emp

The document discusses three types of rank functions in SQL: ROW_NUMBER(), DENSE_RANK(), and RANK(). It provides examples of how to use each function to assign ranks at the table level or partitioned by a group like job. ROW_NUMBER() returns unique ranks for all rows. DENSE_RANK() returns unique ranks for distinct values and the same rank for duplicates without gaps. RANK() also returns unique ranks for distinct values and the same rank for duplicates, but maintains gaps between duplicate ranks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views7 pages

Rank Functions:: Select Ename, Job, Sal, Row - Number Over (Order by Sal Desc) RN From Emp

The document discusses three types of rank functions in SQL: ROW_NUMBER(), DENSE_RANK(), and RANK(). It provides examples of how to use each function to assign ranks at the table level or partitioned by a group like job. ROW_NUMBER() returns unique ranks for all rows. DENSE_RANK() returns unique ranks for distinct values and the same rank for duplicates without gaps. RANK() also returns unique ranks for distinct values and the same rank for duplicates, but maintains gaps between duplicate ranks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

RANK FUNCTIONS:

Rank functions are three types:

1. Row_number()
2. Dense_rank()
3. Rank()

We can apply ranks at table level or at any particular group level also like job level.

We use ‘partition by’ to apply ranks at any group level.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL OMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- -------------------------------------------------------

7369 SMITH CLERK 7902 17-DEC-80 800 20


7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10

14 rows selected.

ROW_NUMBER(): this functions returns unique ranks for all values including duplicates also.

It ignores duplicate values.

--AT TABLE LEVEL:

SQL> select ename,job,sal,row_number() over(order by sal desc) rn from emp;

ENAME JOB SAL RN


---------- --------- ---------- --------------------- -
KING PRESIDENT 5000 1
FORD ANALYST 3000 2
SCOTT ANALYST 3000 3
JONES MANAGER 2975 4
BLAKE MANAGER 2850 5
CLARK MANAGER 2450 6
ALLEN SALESMAN 1600 7
TURNER SALESMAN 1500 8
MILLER CLERK 1300 9
WARD SALESMAN 1250 10
MARTIN SALESMAN 1250 11
ADAMS CLERK 1100 12
JAMES CLERK 950 13
SMITH CLERK 800 14

14 rows selected.

--AT JOB LEVEL:

SQL> select ename,job,sal,row_number() over(partition by job order by sal


desc) rn from emp;
ENAME JOB SAL RN
---------- --------- ---------- ----------------------
FORD ANALYST 3000 1
SCOTT ANALYST 3000 2
MILLER CLERK 1300 1
ADAMS CLERK 1100 2
JAMES CLERK 950 3
SMITH CLERK 800 4
JONES MANAGER 2975 1
BLAKE MANAGER 2850 2
CLARK MANAGER 2450 3
KING PRESIDENT 5000 1
ALLEN SALESMAN 1600 1
TURNER SALESMAN 1500 2
MARTIN SALESMAN 1250 3
WARD SALESMAN 1250 4

14 rows selected.
DENSE_RANK()

This function returns unique rank for distinct values and same rank for duplicate values.

After same rank, this function continues the next rank, means it will not maintain any gaps
between ranks.

--AT TABLE LEVEL

SQL> select ename,job,sal,dense_rank() over(order by sal desc) dn from emp;


ENAME JOB SAL DN
---------- --------- ---------- --------------------
KING PRESIDENT 5000 1
FORD ANALYST 3000 2
SCOTT ANALYST 3000 2
JONES MANAGER 2975 3
BLAKE MANAGER 2850 4
CLARK MANAGER 2450 5
ALLEN SALESMAN 1600 6
TURNER SALESMAN 1500 7
MILLER CLERK 1300 8
ARD SALESMAN 1250 9
MARTIN SALESMAN 1250 9
ADAMS CLERK 1100 10
JAMES CLERK 950 11
SMITH CLERK 800 12

14 rows selected.

--AT JOB LEVEL:

SQL> select ename,job,sal,dense_rank() over(partition by job order by sal desc)


dn from emp;
ENAME JOB SAL DN
---------- --------- ---------- -------------------
FORD ANALYST 3000 1
SCOTT ANALYST 3000 1
MILLER CLERK 1300 1
ADAMS CLERK 1100 2
JAMES CLERK 950 3
SMITH CLERK 800 4
JONES MANAGER 2975 1
BLAKE MANAGER 2850 2
CLARK MANAGER 2450 3
KING PRESIDENT 5000 1
ALLEN SALESMAN 1600 1
TURNER SALESMAN 1500 2
MARTIN SALESMAN 1250 3
WARD SALESMAN 1250 3

14 rows selected.

By using dense_rank(), we can find nth max or min salary also.

Display ename who is taking 5th max sal in the emp table

SQL> select ename from (select ename,job,sal,dense_rank() over(order by sal desc) dn from emp) tt
where dn=5;

ENAME
----------
CLARK

Display emp details who are taking max salaries in each job

SQL> select * from (select ename,job,sal,dense_rank() over(partition by job order by sal desc) dn from
emp) where dn=1;

ENAME JOB SAL DN


---------- --------- ---------- -------------------
SCOTT ANALYST 3000 1
FORD ANALYST 3000 1
MILLER CLERK 1300 1
JONES MANAGER 2975 1
KING PRESIDENT 5000 1
ALLEN SALESMAN 1600 1

6 rows selected.

For 2nd max salaries:

SQL> select * from (select ename,job,sal,dense_rank() over(partition by job order by sal desc) dn from
emp) where dn=2;
ENAME JOB SAL DN
---------- --------- ---------- -----------------------
ADAMS CLERK 1100 2
BLAKE MANAGER 2850 2
TURNER SALESMAN 1500 2

For 3rd max salaries

SQL> select * from (select ename,job,sal,dense_rank() over(partition by job order by sal desc) dn from
emp) where dn=3;

ENAME JOB SAL DN


---------- --------- ---------- ----------------------
JAMES CLERK 950 3
CLARK MANAGER 2450 3
WARD SALESMAN 1250 3
MARTIN SALESMAN 1250 3

Display emp details who are very low salaries from emp table:

SQL> select * from (select ename,job,sal,dense_rank() over(partition by job order by sal) dn from emp)
where dn=1;

ENAME JOB SAL DN


---------- --------- ---------- -----------------
SCOTT ANALYST 3000 1
FORD ANALYST 3000 1
SMITH CLERK 800 1
CLARK MANAGER 2450 1
KING PRESIDENT 5000 1
WARD SALESMAN 1250 1
MARTIN SALESMAN 1250 1

7 rows selected.

Display emp details who are very low salaries from each job:

SQL> select * from (select ename,job,sal,dense_rank() over(partition by job order by sal) dn from emp)
where dn=1;

ENAME JOB SAL DN


---------- --------- ---------- ----------------
SCOTT ANALYST 3000 1
FORD ANALYST 3000 1
SMITH CLERK 800 1
CLARK MANAGER 2450 1
KING PRESIDENT 5000 1
WARD SALESMAN 1250 1
MARTIN SALESMAN 1250 1

7 rows selected.

RANK()
This function gives unique rank for distinct values and same rank for duplicate values, but after
same rank it will maintain gaps in the ranks.

--AT TABLE LEVEL

SQL> select ename,job,sal,rank() over(order by sal desc) rnk from emp;


ENAME JOB SAL RNK
---------- --------- ---------- ---------------------
KING PRESIDENT 5000 1
FORD ANALYST 3000 2
SCOTT ANALYST 3000 2
JONES MANAGER 2975 4
BLAKE MANAGER 2850 5
CLARK MANAGER 2450 6
ALLEN SALESMAN 1600 7
TURNER SALESMAN 1500 8
MILLER CLERK 1300 9
WARD SALESMAN 1250 10
MARTIN SALESMAN 1250 10
ADAMS CLERK 1100 12
JAMES CLERK 950 13
SMITH CLERK 800 14

14 rows selected.

--AT JOB LEVEL

SQL> select ename,job,sal,rank() over(partition by job order by sal desc) rnk


from emp;
ENAME JOB SAL RNK
---------- --------- ---------- --------------------
FORD ANALYST 3000 1
SCOTT ANALYST 3000 1
MILLER CLERK 1300 1
ADAMS CLERK 1100 2
JAMES CLERK 950 3
SMITH CLERK 800 4
JONES MANAGER 2975 1
BLAKE MANAGER 2850 2
CLARK MANAGER 2450 3
KING PRESIDENT 5000 1
ALLEN SALESMAN 1600 1
TURNER SALESMAN 1500 2
MARTIN SALESMAN 1250 3
WARD SALESMAN 1250 3

14 rows selected.

You might also like