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

Explain what are the events recorded in a transaction log?

569


What is exclusive locks?

545


How do you make a trace?

593


explain declarative management framework (dmf) in sql server 2008?

601


How many replicas are maintained for each sql azure db?

106






What is an inner join?

539


How to integrate the ssrs reports in application?

550


What is the order by used for?

633


How to connect to SQL Azure Database by using sqlcmd?

110


Which table keeps information about stored procedures?

545


Can you always create a cache of a report?

117


What are window functions in sql server?

520


What command would you use to add a column to a table in sql server?

758


How to get a list of columns using the "sp_help" stored procedure in ms sql server?

590


What is normalization? Describe its different types.

601