Query to find Top N records from an Table w/o TOP function


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Query to find Top N records from an Table w/o TOP function

  1. #1
    Kedar Guest

    Query to find Top N records from an Table w/o TOP function


    I want to find out a query for an Oracle database which will give top n records
    from table.
    suppose I have a Employee table which has 4 fields viz. Ename,Age,Dept &
    Salary, now I want to find Top three salaries from Table which query will
    be most appropriate?

  2. #2
    Joe \Nuke Me Xemu\ Foster Guest

    Re: Query to find Top N records from an Table w/o TOP function

    "Kedar" <kedar_aj@hotmail.com> wrote in message <news:3bf65d24$1@147.208.176.211>...

    > I want to find out a query for an Oracle database which will give top n records
    > from table.
    > suppose I have a Employee table which has 4 fields viz. Ename,Age,Dept &
    > Salary, now I want to find Top three salaries from Table which query will
    > be most appropriate?


    Maybe you can do a self-join with grouping: (untested)

    select employee.id, min(employee.name), min(employee.salary)
    from employee, employee as e
    where employee.salary <= e.salary
    group by employee.id
    having count(*) <= n

    --
    Joe Foster <mailto:jlfoster%40znet.com> Got Thetans? <http://www.xenu.net/>
    WARNING: I cannot be held responsible for the above They're coming to
    because my cats have apparently learned to type. take me away, ha ha!



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center