Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL Question.
>I wonder if there is a way to select, for instance, the last 10 rows of
>a table. I know that i can select the first 10 rows by:
>
>SELECT *
>FROM CUSTOMERS
>WHERE ROWNUM<10;
>
>Thanks in advance.
>Roberto Pérez Lita.
If you use the following code:
SELECT *
FROM CUSTOMERS
ORDER BY CUST_ID
WHERE ROWNUM<11;
and CUST_ID is a Primary Key then the order in which the rows are returned will give you the first 10 rows.
Similarly if you use :
SELECT *
FROM CUSTOMERS
ORDER BY CUST_ID DESC
WHERE ROWNUM<11;
then this would give you the last 10 rows.
This is because Oracle assigns ROWNUM in the order in which the rows are retrieved. If it uses an index to retrieve the rows (and it should if you are using a primary key) then the result should be as expected above. I have tried it and it seems to work.
Terry Received on Fri Jun 11 1999 - 04:48:44 CDT
![]() |
![]() |