Tuesday 6 May 2014

SQL ROWNUM

SQL ROWNUM

Rownum :
It is an unique value
It is an dynamic value automatically retrieved along with Select statement output.
It is only for display purpose.
It is not stored in database.

SQL >select rownum,ename,sal from emp;
SQL >select rownum,dname,loc from dept;

Retrieving Top 5 Highly paid Employees
SQL > select rownum,empno,ename,job,sal from
(select rownum,empno,ename,job,sal from emp
order by sal desc )
where rownum <= 5;

Retrieving  Nth maximum salaried employ details (2 max,...)
SQL > select rownum,empno,ename,job,sal from
(select rownum,empno,ename,job,sal from emp
order by sal desc )
group by rownum,empno,ename,job,sal
having rownum = &N;

Retrieving Alternate rows
SQL>select rownum,empno,ename,job,sal from emp
group by rownum,empno,ename,job,sal
having mod(rownum,2) = 0; -- EVEN Rows
[ having mod(rownum,2) != 0; ] -- ODD Rows

Inline view: Select statement provided in place of table name is known as Inline view.