0% found this document useful (0 votes)
579 views5 pages

Rowid: ROWID Is A Pseudo-Column Which Will Be Present For Every Table in Oracle

ROWID is a pseudo-column present for every table in Oracle that returns the address of each row. This address contains the data object number, data block, row position, and datafile of the row. Rowid values uniquely identify rows and can be used to access rows fastest, see how rows are stored, and as unique identifiers. ROWNUM is another pseudo-column that returns the number indicating the order Oracle selects each row of a query. It can be used to limit the number of records selected. Neither ROWID nor ROWNUM values can be inserted, updated, or deleted.

Uploaded by

Vasanth
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)
579 views5 pages

Rowid: ROWID Is A Pseudo-Column Which Will Be Present For Every Table in Oracle

ROWID is a pseudo-column present for every table in Oracle that returns the address of each row. This address contains the data object number, data block, row position, and datafile of the row. Rowid values uniquely identify rows and can be used to access rows fastest, see how rows are stored, and as unique identifiers. ROWNUM is another pseudo-column that returns the number indicating the order Oracle selects each row of a query. It can be used to limit the number of records selected. Neither ROWID nor ROWNUM values can be inserted, updated, or deleted.

Uploaded by

Vasanth
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/ 5

ROWID :

ROWID is a pseudo-column which will be present for every table in oracle.

For each row in the database, the ROWID pseudo-column returns the address of the row. Oracle
Database rowid values contain information necessary to locate a row:

 The data object number of the object


 The data block in the datafile in which the row resides
 The position of the row in the data block (first row is 0)
 The datafile in which the row resides (first file is 1). The file number is relative to the tablespace

Usually, a rowid value uniquely identifies a row in the database. Values of the ROWID pseudo-
column have the datatype ROWID

Rowid values have several important uses:

 They are the fastest way to access a single row.


 They can show you how the rows in a table are stored.
 They are unique identifiers for rows in a table.

NOTE:

1. we should not use ROWID as Primary Key.

2. we can use the ROWID pseudo-column in the SELECT and WHERE clause of a query, these
pseudo-column values are not actually stored in the database. You cannot insert, update, or
delete a value of the ROWID pseudo-column.

EXAMPLE:

Select rowid , ename ,sal


From emp;

It gives the address of all the rows present in emp table.


ROWNUM:

ROWNUM is also a pseudo-column.


For each row returned by a query, the ROWNUM pseudo-column returns a number indicating the
order in which Oracle selects the row from a table or set of joined rows. The first row selected
has a ROWNUM of 1, the second has 2, and so on.

NOTE:
1. We can use the rownum pseudo-column in where clause in-order to limit the number of
records to be selected by a query.

Example:

Select * from emp


Where rownum <=5;

It gives first 5 records from the employee table.

2. Condition checking for rownum values greater than a positive integer will always return false to
where clause and the query select no record from the table.

Example:

Select * from emp


Where rownum>1;

It gives no records.

3. If an order-by clause follows rownum in the same query, then the rows will be reordered
by the order-by clause after assigning rownum.

select rownum,ename,sal
from emp
order by sal;

ROWNUM ENAME SAL


---------- ---------- ----------
2 SMITH 800
13 JAMES 950
12 ADAMS 1100
4 WARD 1250
6 MARTIN 1250
15 MILLER 1300
11 TURNER 1500
3 ALLEN 1600
8 CLARK 2450
7 BLAKE 2850
5 JONES 2975
14 FORD 3000
9 SCOTT 3000
10 KING 5000
* to avoid the above problem we can use a subquery in the from clause as follows:

Select rownum , ename, sal


From (select ename , sal from emp order by sal);

ROWNUM ENAME SAL


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

Queries on Rownum:

1. To display first N records from the table.

Select *
From emp
Where rownum <=&N ;

2. To display first half records from the table.

Select *
From emp
Where rownum <=( select count(*)/2 from emp);

3. To display Nth record from the table.

Select *
From emp
Where rownum <= &N
Minus
Select *
From emp
Where rownum < &N ;
4. To display bottom N records from the table.

Select *
From emp
Minus
Select *
From emp
Where rownum <= (select count(*) - &N from emp) ;

5. To display Nth max value in a table.

Example : 1. To obtain details of employee who earn 1st max salary in emp table.

Select *
From emp
Where sal in (Select sal
From ( select distinct sal from emp where sal is not null order by sal desc)
Where rownum = 1);

2. To obtain details of employee who earn 2nd max salary in emp table.

Select *
from emp
where sal in (Select sal
From ( select distinct sal from emp where sal is not null order by sal desc)
Where rownum <=2
Minus
Select *
From ( select distinct sal from emp where sal is not null order by sal desc)
Where rownum <2);

Therefore, the query to display employee details who earn Nth max salary.

Select *
from emp
where sal in (Select sal
From ( select distinct sal from emp where sal is not null order by sal desc)
Where rownum <=&N
Minus
Select *
From ( select distinct sal from emp where sal is not null order by sal desc)
Where rownum <N);
Therefore, the query to display employee details who earn Nth max salary.

Select *
from emp
where sal in (Select sal
From ( select distinct sal from emp where sal is not null order by sal asc)
Where rownum <=&N
Minus
Select *
From ( select distinct sal from emp where sal is not null order by sal asc)
Where rownum <&N);

You might also like