IT Community - Software Programming, Web Development and Technical Support

How do you return the top-N results of a query in Oracle?

This is a discussion on How do you return the top-N results of a query in Oracle? within the Database Support forums, part of the Web Development category; How do you return the top-N results of a query in Oracle? Why doesn’t the obvious method work?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-16-2007, 11:39 PM
vadivelanvaidyanathan vadivelanvaidyanathan is offline
D-Web Genius
 
Join Date: Feb 2007
Posts: 803
vadivelanvaidyanathan is on a distinguished road
Question How do you return the top-N results of a query in Oracle?

How do you return the top-N results of a query in Oracle? Why doesn’t the obvious method work?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-17-2007, 01:03 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: How do you return the top-N results of a query in Oracle?

Hi Vadivel, what do mean by obvious method !

Here are the few examples of how the top-n results are fetched from table...


-- Create table
CREATE TABLE EMPL (ID NUMBER, ENAME VARCHAR2(30), SALARY INTEGER);

-- Inserting values
INSERT INTO EMPL VALUES(1,'Raj', 10500);
INSERT INTO EMPL VALUES(2,'Kumar', 21000);
INSERT INTO EMPL VALUES(3,'Selvam', 30000);
INSERT INTO EMPL VALUES(4,'John', 7500);
INSERT INTO EMPL VALUES(5,'Smith', 35000);
INSERT INTO EMPL VALUES(6,'Vadivel', 40000);
INSERT INTO EMPL VALUES(7,'Xyx', 10000);



Method-1:
----------

SELECT ROWNUM as Rank, ENAME, SALARY FROM
(SELECT ROWNUM, ENAME, SALARY FROM empl ORDER BY SALARY DESC) WHERE ROWNUM <= 3;

RANK ENAME SALARY
---------- ------------------------------ ----------
1 Vadivel 40000
2 Smith 35000
3 Selvam 30000

3 rows selected.


Method-2:
---------

SELECT ENAME,SALARY, RANK() OVER (ORDER BY SALARY DESC) RANK FROM EMPL;


------------------------------ ---------- ----------
ENAME SALARY RANK
------------------------------ ---------- ----------
Vadivel 40000 1
Smith 35000 2
Selvam 30000 3
Kumar 21000 4
Raj 10500 5
Xyx 10000 6
John 7500 7

7 rows selected.

-- If you want to restrict the result set to top 3 rank then use the below query..

SELECT ENAME,SALARY,RANK FROM (SELECT ENAME,SALARY, RANK() OVER (ORDER BY SALARY DESC) RANK FROM EMPL ) WHERE RANK <= 3


ENAME SALARY RANK
------------------------------ ---------- ----------
Vadivel 40000 1
Smith 35000 2
Selvam 30000 3

3 rows selected.


I feel the above given methods are obvious !!!
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-07-2007, 07:56 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Default Re: How do you return the top-N results of a query in Oracle?

Hi there,

Here is an example for finding the maximum salary from an employee table...

Use Pubs
Go
Create table Employee
(
EmpID int,
EmpName varchar(10),
EmpSalary money
)
Go
Insert into Employee values (1,'Shilpa',2874)
Insert into Employee values (2,'Mahesh',3712)
Insert into Employee values (3,'Vineela',4222)
Insert into Employee values (4,'Supriya',6500)
Insert into Employee values (5,'Latha',6522)
Go

The Query for the Maximum Salary goes as follows:

Select * From Employee Where Empsalary = (Select max(EmpSalary) From Employee)
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Oracle query do display users Jeyaseelansarc PHP Programming 0 09-28-2007 05:35 AM
need a query Oracle DB. H2o Database Support 11 08-09-2007 11:38 PM
SQL query in Oracle 8 vigneshgets Software Testing 0 05-17-2007 05:27 AM
Supplemental Results Joe Google 0 02-23-2007 06:45 AM
Supplemental Results Joe Google 1 02-21-2007 01:20 AM


All times are GMT -7. The time now is 04:34 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0