wat will be the sql query to extract only last 3 records
from table supose table hving thousands for records

Answer Posted / shubhra

This query will give last three record in the table in the
same order.

CREATE TABLE dbo.Test1 (
[ID] [int] ,
[FirstName] [varchar](25),
[LastName] [varchar](25)
) ON [PRIMARY]

INSERT INTO Test1 VALUES(1, 'Bob','Smith')
INSERT INTO Test1 VALUES(2, 'Dave','Jones')
INSERT INTO Test1 VALUES(3, 'Karen','White')
INSERT INTO Test1 VALUES(1, 'Bob','Smith')
INSERT INTO Test1 VALUES(4, 'Bobby','Smita')

select identity(int,1,1) as SlNo,* into #temp from Test1

select * from (select top 3 * from #temp order by slno
desc) a order by slno
drop table #temp

Is This Answer Correct ?    1 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to automatically create a log when an exception is being received into SQL Server?

947


How to recreate an existing index in ms sql server?

738


What is the stuff?

728


What is dml command?

723


What is the advantage of sql server?

750


Describe in brief authentication modes in sql server.

741


What is an indexing strategy?

720


what is a join? : Sql server database administration

773


Can an automatic recovery be initiated by a user?

774


What is the most common trace flags used with sql server?

700


What is the difference between ‘having’ clause and a ‘where’ clause?

788


How would we use distinct statement? What is its use?

803


What is user-defined scalar function?

737


What is sql view?

752


What is a raid and what are different types of raid configurations?

863