How to get Last row (record) from a table


Here i will tell you how to get last row of a table, this is simple a trick ,

In this example i have created temporary table,first i have created a table
then i have inserted some records into #student table



CREATE TABLE #STUDENT(ID BIGINT IDENTITY,FULLNAME NVARCHAR(MAX),AGE INT,GENDER CHAR(1),STATUS BIT DEFAULT(0))

insert some records into #student table

INSERT INTO #STUDENT VALUES('Aamir Hasan',25,'M',0)
INSERT INTO #STUDENT VALUES('Aamir Hasan',24,'M',0)
INSERT INTO #STUDENT VALUES('Saida Hasan',23,'F',0)
INSERT INTO #STUDENT VALUES('Aamir Hasan',22,'M',0)
INSERT INTO #STUDENT VALUES('Saba Hasan',21,'F',0)


Select all records from #student table

SELECT * FROM #student

now here you can get last record from below query.

SELECT TOP 1 * FROM #student ORDER BY id DESC
after all step drop #student table
DROP TABLE #student


Attachments

  • Last row (record) from a table SQL Query (38393-171227-SQL.sql.txt)
  • Related Articles

    More articles: SQL get last row SQL query SQL 2008

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: