Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to select last few records

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-08-18 : 07:57:36
Mack writes "I want to select last 2 records from a table AA which has following 5 records.

111
222
333
444
555

There is no primary key.
Any tip will be really appretiated.
Thanks for taking time to read it."

Kristen
Test

22859 Posts

Posted - 2005-08-18 : 08:00:09
Hi Mack, Welcome to SQL Team!

You will have to declare some sort of ORDER BY to get the "last two" - SQL has no concept of an order to the data - so if you need the last two in order or data entry you will need a "Create Date" column in the record.

SELECT TOP 2 *
FROM MyTable
ORDER BY MyColumn

Kristen
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-18 : 08:11:13
You need to define last two

SELECT TOP 2 *
FROM MyTable
ORDER BY MyColumn DESC

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -