Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT TOP in Ora8

Status
Not open for further replies.

bluewolf

Programmer
Jul 16, 2001
13
VN
Hi all,
In SQL Server 7.0, I can get top 5 records order by date very easy with this syntax:
SELECT TOP 5 column1 FROM table1
WHERE condition1
ORDER BY column2

But in Oracle8, I don't know how to do that. Someone can help me? MCSD BW.
 
You could user the rownum virtual column. This column is not part of any table, but every time a SQL gets executed, a row number (rownum) is associated with each row from the result set. So you could do this to get the first five rows for your SQL:

select * from
(SELECT column1 FROM table1
WHERE condition1
ORDER BY column2)
where rownum < 6;

I wrapped your query into this outer SQL because Oracle has first to execute the ORDER BY on all the inner select before being able to give you the first five rows. If I had put the &quot;where rownum < 6&quot; in your SQL directly, Oracle would have sorted only the first five rows of the result sets.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top