# Database Discussions > MS SQL Server 7/MS SQL Server 2000 >  How to find last 10 records using T-SQL?

## Lee

Hi all,

Anybody would tell me how can I find last 10 records in a table using T-SQL?

Thank you very much!

Lee

----------


## Karl

Lee,

try this:

SELECT TOP 10 * FROM <table name> 
ORDER BY <column name> ASC

The important part of this query is the ORDER BY.  The column that you use to determine what a last record is should go in the ORDER BY clause.  The ASC keyword starts at the last record and makes sure that each subsequent record is higher than the previous one.  The TOP 10 clause speaks for itself really.  

Don&#39;t be misled by the word &#34;TOP&#34;, this isn&#39;t to say that you&#39;re selecting the highest records.  First SQL Server runs the query without the TOP.  The ORDER BY needs to get processed first, and because it is ASCENDING, the lowest record appears at the top of the result list, then the next lowest, and so on.  The TOP keyword then selects the TOP n (in this case 10) records from the result list.

Hope that helps,

Karl


------------
Lee at 9/7/01 2:17:36 PM


Hi all,

Anybody would tell me how can I find last 10 records in a table using T-SQL?

Thank you very much!

Lee

----------


## Lee

Hi Karl,

Thank you so much!!!


------------
Karl at 9/8/01 4:00:41 AM

Lee,

try this:

SELECT TOP 10 * FROM <table name> 
ORDER BY <column name> ASC

The important part of this query is the ORDER BY.  The column that you use to determine what a last record is should go in the ORDER BY clause.  The ASC keyword starts at the last record and makes sure that each subsequent record is higher than the previous one.  The TOP 10 clause speaks for itself really.  

Don&#39;t be misled by the word &#34;TOP&#34;, this isn&#39;t to say that you&#39;re selecting the highest records.  First SQL Server runs the query without the TOP.  The ORDER BY needs to get processed first, and because it is ASCENDING, the lowest record appears at the top of the result list, then the next lowest, and so on.  The TOP keyword then selects the TOP n (in this case 10) records from the result list.

Hope that helps,

Karl


------------
Lee at 9/7/01 2:17:36 PM


Hi all,

Anybody would tell me how can I find last 10 records in a table using T-SQL?

Thank you very much!

Lee

----------


## DBA723

if you are looking for the &#39;last&#39; 10 records, then the statement should be for descending order, not ascending.  this is assuming that you have an identity field or datetime field.


------------
Lee at 9/7/01 2:17:36 PM


Hi all,

Anybody would tell me how can I find last 10 records in a table using T-SQL?

Thank you very much!

Lee

----------

