Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1916
  • Last Modified:

selecting first and last records from a result set

how do i select top x , bottom x rows in a select query.

i mean something like
select * from <blah blah> where <blah blah> first 10;

 
select * from <blah blah> where <blah blah> last 10;

0
k_suchdeva
Asked:
k_suchdeva
  • 3
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
This depends largely from the database system you are using. Anyway, when you say "first" and "last", you also need to specify the "ordering".

Here is the suggestion for SQL Server:
select top 10 * from yourtable order by somefield asc
union all
select top 10 * from yourtable order by somefield desc

or
set rowcount 10
select * from yourtable order by somefield asc
union all
select * from yourtable order by somefield desc
set rowcount 0

CHeers
0
 
k_suchdevaAuthor Commented:
I have tried these two already.
SOrry forgot to mention my datbaase is oracle and i am trying it from sqlplus.
select top 10 * from <table>;
select top 10 * from <table>
           *
ERROR at line 1:
ORA-00923: FROM keyword not found where expected


set rowcount 10
SP2-0158: unknown SET option "rowcount"

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Check out this:

select * from yourtable where rownum <=10 order by somefield asc

select * from yourtable where rownum <=10 order by somefield desc

!!!! UNTESTED !!!!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DrSQLCommented:
k_suchdeva,
   As long as you are on Oracle 8i or later, you can use the analytic functions (angelll's solution won't work since rownum is assigned prior to the sort):

Top 10:
select * from
  (select <blah column 1>, <blah column 2>, <etc.>,
          row_number() over (order by <blah col 1>,
                                           <blah col2>) ranking
     from <blah blah>)
 where ranking <= 10;

Bottom 10 (just reverse the order by):
select * from
  (select <blah column 1>, <blah column 2>, <etc.>,
          row_number() over (order by <blah col 1> desc,
                                           <blah col2> desc) ranking
     from <blah blah>)
 where ranking <= 10;

you can also sort the whole thing (takes longer) instead of using these functions, and put it in an inline view:

select * from (select * from <blah blah> order by <blah columns>)
 where rownum <= 10;

and the reverse (putting desc on the columns in the order by) for the bottom 10.

Good luck!
0
 
k_suchdevaAuthor Commented:
Strangely

select * from mr_orders where rownum <=1;
works
while
select * from mr_orders where ranking <=1;
does not

So who wants the points ?
0
 
DrSQLCommented:
k_suchdeva,
    rownum is what's called a "virtual column" that is available to any query as if it was a part of the table being queried (USER and SYSDATE are other examples).  "Ranking" is a column alias that I gave to the row_number function inside the inline view (the select that is inside the parens), so that I could refer to it in the outer query.  I could have called it fred or barney, I just used a name that indicated what it was doing.  That means you can't use "ranking" without the inline view.  So:

select * from
(select ename,sal,row_number() over (order by sal desc) as fred
   from emp)
where fred <= 5;

Will get me the top 5 salaries and:

select * from
(select ename,sal,row_number() over (order by sal) as barney
   from emp)
where barney <= 5;

Will get me the bottom 5.

But, "select * from mr_orders where rownum <=10" will just give me the first 10 received from the mr_orders table.  And:

select * from mr_orders where rownum <=10 order by qty;

Will still give me the first 10, not the top 10.  This is because sorting happens after the result set has been completed, but rownum is assigned as records are added to the result set.  So, to get the top 10 customers (based on number of orders):

select * from
(select custid,count(*) orders,
           row_number() over (order by count(*) desc) ranking
    from mr_orders
   group by custid)
where ranking <= 10;

Good luck!

0
 
k_suchdevaAuthor Commented:
Hi DrSQL
Got it !!
This is one thing i love so much about this site.

People are just passionate about explaining the problem and you get you get exactly the answer you want.

Thanks for ur effort.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now