aspose file tools*
The moose likes JDBC and the fly likes Difference between rowid and rownum in sql query?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Difference between rowid and rownum in sql query??" Watch "Difference between rowid and rownum in sql query??" New topic
Author

Difference between rowid and rownum in sql query??

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,

In sql query what is the difference between rowid and rownum ?

As I know that :

rowid is the unique index number of every row of table maintaining by database automatically.

rownum is the sequential number of rows in the resultset object.

But my confusion is if I write query like below for extracting 1st row

select * from table_name where rownum = 1 Then it is working fine

But if I write query like below for extracting 3rd row

select * from table_name where rownum = 3 Then it is returning zero row.

for that i have to write query like below

select * from table_name where rowid in(
(select rowid from table_name where rownum <= 3)
minus
(select rowid from table_name where rownum < 3)
)

Can any one please clarify me.

Thanks & Regards
Bikash
[ June 03, 2004: Message edited by: Bikash Paul ]
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
rownum is usually used to restrict the number of rows that are returned(due to obvious web based result restrictions like u can process only certain number of rows).

but the followingtip may give you more insight into this issue.

Remember that you can always use google to understand these technical issues quickly.


Kishore
SCJP, blog
Rudy Dakota
Ranch Hand

Joined: Jul 27, 2002
Posts: 54
Hi Paul,

Assuming we 're talking Oracle:

- ROWID is a semi-physical value specifying the location of a row (in terms of data file, extent, etcetera). ROWID is not very useful in day-to-day programming, as it is not a constant value (it changes with table re-organizations, imports, and things like that). Therefore it requires caution in its use.
- ROWNUM is a logical value for the sequence of row retrieval in the resultset. This is the position before sorting, so rownum is not always the same as the position in the resultset presented to you. This also requires some caution.

Hope this helps,

Rudy.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between rowid and rownum in sql query??