If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Top 100 rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Aug 2004
Location: Buenos Aires, Argentina
Posts: 28
Top 100 rows

In SQL Server I had a command, "TOP" with which I could get a number of lines from a table: "SELECT TOP 100 from TABLE1"
Is there an equivalent in Oracle?
Thank you!
Reply With Quote
  #2 (permalink)  
Old
Registered User
 
Join Date: Jan 2005
Location: Green Bay
Posts: 201
you can stop the record set return at any number of rows with the following

where rownum <= 100

This will give you 100 rows of output in no paticular order.
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Aug 2004
Location: Buenos Aires, Argentina
Posts: 28
Worked perfectly. Thanks!
Reply With Quote
  #4 (permalink)  
Old
Lead Application Develope
 
Join Date: Jun 2004
Location: Liverpool, NY USA
Posts: 2,419
However, the rows returned will be RANDOM. If you want a sorted list of the top 100 then

select col1
from
(select col1
from mytable
order by col1)
where rownum <= 100;
__________________
Bill
You do not need a parachute to skydive. You only need a parachute to skydive twice.
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Dec 2003
Location: Oklahoma, USA
Posts: 354
Right, if you simply specify --

SELECT col1 FROM my_table WHERE rownum < 100;

what you will get are the first 100 records stored in the table, in no particular order. I've had developers try and "work around" it and do something like --

SELECT col1 FROM my_table WHERE rownum < 100 ORDER BY col1;

which looks like it will work, but doesn't because the rownum being a part of the WHERE clause gets evaluated before the ORDER BY clause.

Hence, the solution offered by beilstwh is the best solution... btw, Oracle calls that a "Top-N Query".
__________________
JoeB
save disk space, use smaller fonts
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On