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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Selecting Last Row of a Table

Selecting Last Row of a Table

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelp
4 Posts 4 Posters 1 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.
  • H Offline
    H Offline
    Hassanur
    wrote on last edited by
    #1

    Hi What will be the SQL SELECT statement to select only the last row of a table. I am using Microsoft SQL server. Please Help me. Tutul

    A M 2 Replies Last reply
    0
    • H Hassanur

      Hi What will be the SQL SELECT statement to select only the last row of a table. I am using Microsoft SQL server. Please Help me. Tutul

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      Sorry for my english... You should to create a new field (for example "created_on") in your table, which type is TIMESTAMP. When you inserting a new record in the table this field will automatically filled within the time of creation of the new record... So, the request is: SELECT * FROM myTable ORDER BY created_on DESC LIMIT 1

      U 1 Reply Last reply
      0
      • A Anonymous

        Sorry for my english... You should to create a new field (for example "created_on") in your table, which type is TIMESTAMP. When you inserting a new record in the table this field will automatically filled within the time of creation of the new record... So, the request is: SELECT * FROM myTable ORDER BY created_on DESC LIMIT 1

        U Offline
        U Offline
        User 556471
        wrote on last edited by
        #3

        If the table has a unique ID column (primary key) then u can Select the last record from the table as: Select [Fields Name] from [Table Name] where [Pk Field name] in (Select MAX(Pk Field name) as nID from [Table Name]) e.g., Select employeename,Employee_ID from tbl_emp where Employee_ID in (Select MAX(Employee_id) as nid from tbl_emp) Regards Anyla

        1 Reply Last reply
        0
        • H Hassanur

          Hi What will be the SQL SELECT statement to select only the last row of a table. I am using Microsoft SQL server. Please Help me. Tutul

          M Offline
          M Offline
          Mandar Patankar
          wrote on last edited by
          #4

          Hi If you have a date field in your table or some other field which can sort the recordset.If yes then try this SELECT top 1 * FROM tblA ORDER BY DATE1 DESC or SELECT top 1 * FROM tblA ORDER BY DATE1 ASC i hope this helps Mandar Patankar Microsoft Certified professional

          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