Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL Question.

Re: SQL Question.

From: Terry Sullivan <terry_sullivan_at_sgsgroup.com>
Date: Fri, 11 Jun 1999 10:48:44 +0100
Message-ID: <7jqm0o$iak$1@lure.pipex.net>


>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

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US