Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 11th, 2003, 12:16 AM
ahuja_neeraj ahuja_neeraj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: India
Posts: 20 ahuja_neeraj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 54 sec
Reputation Power: 0
How to imit the output of a query?

Hi friends

Can any one help me in a query where i want to limit the output of a query. For example if a query returns 10 records i want to see only first 5 or last five..

Or

In other words i want the functionality similar to LIMIT clause of MySQL in oracle

thanks
Neeraj

Reply With Quote
  #2  
Old December 11th, 2003, 08:28 AM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
Which version of Oracle are you using? In Oracle 9i you can do this by using sub query in FROM clause of SELECT statement, An example is givn below:

SELECT *
FROM (SELECT empno, ename, sal, deptno FROM emp ORDER BY empno DESC)
WHERE rownum<6
/

Reply With Quote
  #3  
Old December 11th, 2003, 08:33 AM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
above query can be used to display the last five records from the table, to display the first five record from the table use following query:

SELECT * FROM emp
WHERE rownum <6
ORDER by empno
/

Reply With Quote
  #4  
Old December 11th, 2003, 04:44 PM
Dan Drillich Dan Drillich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 68 Dan Drillich User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Just to be sure that we are on the same page:

Shafique's query:

SELECT * FROM emp
WHERE rownum <6
ORDER by empno
/

will return 5 arbitrary rows from the emp table and these 5 rows will be ordered by empno.
Let's be very careful with rownum - It is assigned only when the query is _fetched_.
On second thought, maybe it's not that confusing: after all the where clause is always executed before the order clause.
Right?

Cheers,
Dan

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > How to imit the output of a query?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway