Wednesday, April 16, 2014

Displaying N th record from Table

SQL> select * from(select rownum rn,e.* from emp e) where rn=&rn;
Enter value for rn: 1
old   1: select * from(select rownum rn,e.* from emp e) where rn=&rn
new   1: select * from(select rownum rn,e.* from emp e) where rn=1

   RN EMPNO ENAME      JOB         MGR HIREDATE    SAL  COMM DEPTNO
----- ----- ---------- --------- ----- --------- ----- ----- ------
    1  7369 SMITH      CLERK      7902 17-DEC-80   800           20

SQL> /
Enter value for rn: 5
old   1: select * from(select rownum rn,e.* from emp e) where rn=&rn
new   1: select * from(select rownum rn,e.* from emp e) where rn=5

   RN EMPNO ENAME      JOB         MGR HIREDATE    SAL  COMM DEPTNO
----- ----- ---------- --------- ----- --------- ----- ----- ------
    5  7654 MARTIN     SALESMAN   7698 28-SEP-81  1250  1400     30

SQL> /
Enter value for rn: 14
old   1: select * from(select rownum rn,e.* from emp e) where rn=&rn
new   1: select * from(select rownum rn,e.* from emp e) where rn=14

   RN EMPNO ENAME      JOB         MGR HIREDATE    SAL  COMM DEPTNO
----- ----- ---------- --------- ----- --------- ----- ----- ------
   14  7934 MILLER     CLERK      7782 23-JAN-82  1300           10


To display 3rd record from emp table

SQL> select * from emp where rownum<=3 minus select * from emp where rownum<3;

EMPNO ENAME      JOB         MGR HIREDATE    SAL  COMM DEPTNO
----- ---------- --------- ----- --------- ----- ----- ------

 7521 WARD       SALESMAN   7698 22-FEB-81  1250   500     30

No comments:

Post a Comment