Home » RDBMS Server » Server Administration » Limit records returned by a select
Limit records returned by a select [message #374699] Mon, 25 June 2001 11:02 Go to next message
Muriel BRETONNEAU
Messages: 3
Registered: June 2001
Junior Member
Is there a way to limit the number of records
returned by a select ? (Oracle 8.1.6)
Can you send me a sample ?
Thanks
Muriel
Re: Limit records returned by a select [message #374701 is a reply to message #374699] Mon, 25 June 2001 11:53 Go to previous messageGo to next message
Andrew again...
Messages: 270
Registered: July 2000
Senior Member
select * from dept
where rownum < 5;
Re: Limit records returned by a select - with order by [message #374727 is a reply to message #374701] Wed, 27 June 2001 02:51 Go to previous message
Jeff Nichols
Messages: 1
Registered: June 2001
Junior Member
that limits the rows returned BEFORE any order by clause is applied -- not what you might think or hope.

To limit the rows returned AFTER an order by clause you have to put the order by in a subquery and then applie the rownum condition to the sorted results of the subquery:

SELECT my_num FROM (SELECT my_num FROM my_table ORDER BY my_num DESC) WHERE rownum <= 1;

This will have one result row with the highest value for my_num. It's a lame example because you could do the same with max(my_num) but you get the picture... See the Link below for Oracle docs on ROWNUM (requires free registration).

Previous Topic: Sum the same records
Next Topic: outer joins
Goto Forum:
  


Current Time: Mon Dec 23 18:10:42 CST 2024