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

Difference between report and query parameter. Why do we need different type of parameter?

565


If we delete pack Spec what will be the status of pack Body ?

966


What are trace flags?

594


What is triggers and its types?

540


What are the steps you can take to avoid “deadlocks”?

521






What are indexes in sql?

574


What is the order by used for?

617


What is the default Port No on which SQL Server listens?

623


What is sqlcmd?

553


You have a table with close to 100 million records recently, a huge amount of this data was updated now, various queries against this table have slowed down considerably what is the quickest option to remedy the situation?

481


Explain important index characteristics?

553


Is it possible to replicate data from sql server to oracle? : sql server replication

543


What is optimization and its types?

534


What is the difference between a unique key and primary key?

513


Differentiate sql server reporting services vs. Crystal reports?

150