SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot Wilmot's Avatar
    Join Date
    Feb 2000
    Location
    Brisbane, Queensland, Australia
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display first X rows with SQL

    Hi, I am having a problem with an SQL query and a friend has a book of mine I know has the answer. So I decided to ask here instead.

    I am using JSP with an Access database and need to make an SQL query which returns the first four results in a resultset.

    I know I could just get all the results, and only take the first four results from the set, but I figured it is more efficient if I use an SQL query which only returns the first four results.

    The query which returns everything is
    SELECT *
    FROM TableName
    ORDER BY FieldName desc;

    Would be great if someone could help me with the count function or whatever to only return the first four results from the above query.

    Thanks
    Brad Culbert
    SQL Server 2005 Books
    www.SQLServer2005Books.com - Reader-rated SQL Server 2005 Books

  2. #2
    SitePoint Enthusiast cjam4's Avatar
    Join Date
    Jul 2001
    Location
    NJ
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Wilmot,

    Try something like this:

    SELECT TOP 4 tableItem
    FROM tableNAME
    ORDER by tblItem desc

    Hope that helps

    Charles


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •