• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Top N Analysis

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
a simple query..
How to find top N salary from emp table, without using sub queries and inline view.....
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Order by the field that you want the top in, then use the "rownum" keyword in the where clause.
As is
Select name, salary
from emp
where rownum < 10
order by salary desc

Mark
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi thanks for reply...
But i think rownum is assigned every time you reorder the records in cursors.therefore if im using order by after row num then new row num will be assigned after applying oreder by...
correct me if im wrong
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
negative, Rownum is not determined till after the ordering is done.
Mark
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using rownums here can be a bit problematic if you're concerned about ties (since many employees can have the same salary). Oracle does have special support for top n queries taking ties into account. Unless they've reorganized the docs since I last looked at it a few years ago this is described in the "data warehousing guide" in the Oracle DB doc set. Look for words like "rank" and "ranking"
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

negative, Rownum is not determined till after the ordering is done.



I don't think this is correct. You have to use a subquery to get the results you want, because sorting happens after the rownum assignment as so:

select name, salary
from (select name, salary
from emp
order by salary desc)
where rownum<10

I remember having a question like this for the 007 cert exam.

John
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic