RANK and DENSE_RANK Analytic Functions
Simple examples of how to use the RANK and DENSE_RANK analytic functions.
oracle miscconfigurationintermediate
by OracleDba
28 views
Simple examples of how to use the RANK and DENSE_RANK analytic functions.
12345678910111213141516171819202122232425262728
--drop table emp purge;
create table emp (
empno number(4) constraint pk_emp primary key,
ename varchar2(10),
job varchar2(9),
mgr number(4),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2)
);
insert into emp values (7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,null,20);
insert into emp values (7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30);
insert into emp values (7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30);
insert into emp values (7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,null,20);
insert into emp values (7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30);
insert into emp values (7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,null,30);
insert into emp values (7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,null,10);
insert into emp values (7788,'SCOTT','ANALYST',7566,to_date('13-JUL-87','dd-mm-rr')-85,3000,null,20);
insert into emp values (7839,'KING','PRESIDENT',null,to_date('17-11-1981','dd-mm-yyyy'),5000,null,10);
insert into emp values (7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30);
insert into emp values (7876,'ADAMS','CLERK',7788,to_date('13-JUL-87', 'dd-mm-rr')-51,1100,null,20);
insert into emp values (7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,null,30);
insert into emp values (7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,null,20);
insert into emp values (7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,null,10);
commit;12345678910111213141516171819202122232425262728293031323334353637383940414243444546
RANK() OVER ([ query_partition_clause ] order_by_clause)
select empno,
deptno,
sal,
rank() over (partition by deptno order by sal) as myrank
from emp;
EMPNO DEPTNO SAL MYRANK
---------- ---------- ---------- ----------
7934 10 1300 1
7782 10 2450 2
7839 10 5000 3
7369 20 800 1
7876 20 1100 2
7566 20 2975 3
7788 20 3000 4
7902 20 3000 4
7900 30 950 1
7654 30 1250 2
7521 30 1250 2
7844 30 1500 4
7499 30 1600 5
7698 30 2850 6
SQL>
select *
from (select empno,
deptno,
sal,
rank() over (partition by deptno order by sal) as myrank
from emp)
where myrank <= 2;
EMPNO DEPTNO SAL MYRANK
---------- ---------- ---------- ----------
7934 10 1300 1
7782 10 2450 2
7369 20 800 1
7876 20 1100 2
7900 30 950 1
7521 30 1250 2
7654 30 1250 2
SQL>12345678910111213141516171819202122232425262728293031323334353637383940414243444546
DENSE_RANK() OVER([ query_partition_clause ] order_by_clause)
select empno,
deptno,
sal,
dense_rank() over (partition by deptno order by sal) as myrank
from emp;
EMPNO DEPTNO SAL MYRANK
---------- ---------- ---------- ----------
7934 10 1300 1
7782 10 2450 2
7839 10 5000 3
7369 20 800 1
7876 20 1100 2
7566 20 2975 3
7788 20 3000 4
7902 20 3000 4
7900 30 950 1
7654 30 1250 2
7521 30 1250 2
7844 30 1500 3
7499 30 1600 4
7698 30 2850 5
SQL>
select *
from (select empno,
deptno,
sal,
dense_rank() over (partition by deptno order by sal desc) as myrank
from emp)
where myrank <= 2;
EMPNO DEPTNO SAL MYRANK
---------- ---------- ---------- ----------
7839 10 5000 1
7782 10 2450 2
7788 20 3000 1
7902 20 3000 1
7566 20 2975 2
7698 30 2850 1
7499 30 1600 2
SQL>12345678910111213141516171819202122
select rank(2000) within group (order by sal) as row_rank
from emp;
ROW_RANK
----------
9
SQL>
select deptno,
rank(2000) within group (order by sal) as row_rank
from emp
group by deptno
order by deptno;
DEPTNO ROW_RANK
---------- ----------
10 2
20 3
30 6
SQL>12345678910111213141516171819202122
select dense_rank(2000) within group (order by sal) as row_rank
from emp;
ROW_RANK
----------
8
SQL>
select deptno,
dense_rank(2000) within group (order by sal) as row_rank
from emp
group by deptno
order by deptno;
DEPTNO ROW_RANK
---------- ----------
10 2
20 3
30 5
SQL>Please to add comments
No comments yet. Be the first to comment!