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

SQL: how can i get the 10 greatest values

Status
Not open for further replies.

erwan

Programmer
Aug 24, 2000
19
0
0
FR
In sql, i want to get only 10 gretest values on a table .

I try with ROWNUM with " where rownum > count(*) -10 but the "order by" is made after the rownum and the result is false.

how can i do that.
thank you!

 
Hi,

U have to use co-related subquery in this context.

Maybe the following sample will help you :

To select top ten salaries from the emp table :

SELECT A.ENAME,A.SAL FROM EMP A
WHERE 10 > (SELECT COUNT(*) FROM EMP B
WHERE A.SAL > B.SAL);

 
The last reply is OK, but should read a.sal<b.sal to do what you need. Also it will possibly be inefficient on a large table. If you have any kind of procedural control you can try the simple, but obvious approach of

select ename, sal
from emp
order by sal desc;

Then fetch the first n rows you're interested in.

Cheers

Nick Warren
 
I've seen this question before. It was posted recently in SQL forum on 8/22/00 by Mackpei. I don't know if it will answer your question directly, but might give you a good starting point.
 
SELECT *
FROM (SELECT *
FROM your_table
ORDER BY your_criteria DESC)
WHERE ROWNUM <= 10;

This will work in Oracle8.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top