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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT TOP... 2

Status
Not open for further replies.

SeAL

Programmer
Mar 15, 2001
139
FR
How can i select top(5) results like in Microsoft SQL :

SELECT top(5) id_person from PERSON;

Is there a way to do it?
 
To select 5 records, You may use rownum in your where clause like rownum <= 5. So far as TOP is concerned you may sort your output in desc or asc ...
e.g
select * from employee
where ronum <= 5
order by emp_id desc

Hope it helps
 
This topic has come up previously. A good reference is thread185-114597.

Kindus's suggestion to add an &quot;order by&quot; clause to the query in order to get the largest emp_ids isn't quite correct. The problem is that Oracle does the order by after applying the where clause, not before. Carp addresses this issue in the earlier thread. If you are on Oracle 8i you can use the technique of embedding the order by clause in a subselect. I think the resulting query would be

SELECT id_person from
(select id_person from PERSON order by id_person desc)
where rownum < 6;
 
thanks karluk, u r right.
I was not aware of this feature ??? that is where clause takes precedence than the order by. I have just verified it myself.
 
Thanks some much guys ;-)
You get my vote!

For all other peeps, those mens are GURU, don't they?

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top