selecting the first 7 out of 1000 rows. How can i do this?

  • Hi all,

    Pls how can I select the first 7 out of 1000 rows of a table?

     

    olanreol@mtnnigeria.net

     

  • select top 7 * from table-name

     


    Kindest Regards,

    Ian Smith

  • Bear in mind that as stated, it will give you the first seven records it happens to find, in no particular order.  Could be the first row inserted, 17th, 159th, etc.  If you want to see them in a particular sequence, you must specify that sequence using an order by clause -

    select top 7 * from table-name order by column_name

    Steve

  • Steve is absolutely right.

    Order by __ ASC\DESC, where __ and even indexing etc. can all play a part depending upon how the table is set up and what the actual output requirement is.

    The phrase x7 of x1000 suggested possible additional breakdowns so I kept it simple, anticipating further response.

     


    Kindest Regards,

    Ian Smith

  • If this 7 is supposed to be dynamic, you might want to do something like

    DECLARE @topnum INT

    SET @topnum = 7

    SET ROWCOUNT @topnum

    SELECT FROM

    ORDER BY

    SET ROWCOUNT 0

    Actually there is no such thing as the first 7 row in a table. So to ensure you get the expected result you definitely need the ORDER BY clause.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply