Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. A SQL query

A SQL query

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shamoon
    wrote on last edited by
    #1

    I got a table mytable in SQL server database. The table contains 20 records. If i execute query select * from mytable, total 20 records are returned. If i want the first four records, i execute the query select top 4 * from mytable, and it returns the first four records. Can anyone tell me about the queries in following cases: 1) Selecting last 4 records from table. 2) Selecting records 9 to 15 from the table.

    N R 2 Replies Last reply
    0
    • S Shamoon

      I got a table mytable in SQL server database. The table contains 20 records. If i execute query select * from mytable, total 20 records are returned. If i want the first four records, i execute the query select top 4 * from mytable, and it returns the first four records. Can anyone tell me about the queries in following cases: 1) Selecting last 4 records from table. 2) Selecting records 9 to 15 from the table.

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      Shamoon wrote: Can anyone tell me about the queries in following cases: 1) Selecting last 4 records from table. 2) Selecting records 9 to 15 from the table. It sounds like you need to page the recordset. If your table contained a primary key that auto-incremented the value this could simply be done by an SQL statement, however this is not normally the case. Check the following: http://www.codeproject.com/asp/rspaging.asp[^] http://www.flws.com.au/showusyourcode/codeLib/code/rstPaging.asp?catID=3[^]


      Nick Parker

      You see the Standards change. - Fellow co-worker

      1 Reply Last reply
      0
      • S Shamoon

        I got a table mytable in SQL server database. The table contains 20 records. If i execute query select * from mytable, total 20 records are returned. If i want the first four records, i execute the query select top 4 * from mytable, and it returns the first four records. Can anyone tell me about the queries in following cases: 1) Selecting last 4 records from table. 2) Selecting records 9 to 15 from the table.

        R Offline
        R Offline
        Rob Graham
        wrote on last edited by
        #3

        The answer to both of the above depends on the table design. If the table has a defined primary key, then you could do 1) as select top 4 * from mytable order by mykey ASC/DESC (whether you need ASCending or Descending depends on how you defined the key...) how to get a range (2) is more challenging if the table contains an identity field (need not be part of the primary key), and records in the table never get deleted, you could use the identity as record index: select * from mytable where myindex > 8 and myindex < 16 otherwise, you would need something cumbersome like: select top 6 * from mytable where myidentity not in (select top 8 myidenty from mytable) if there is no identity field, then the notion of nth record is possibly arbitrary and variable over time...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups