Results 1 to 4 of 4

Thread: How to find last 10 records using T-SQL?

  1. #1
    Lee Guest

    How to find last 10 records using T-SQL?


    Hi all,

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

    Thank you very much!

    Lee

  2. #2
    Karl Guest

    How to find last 10 records using T-SQL? (reply)

    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

  3. #3
    Lee Guest

    How to find last 10 records using T-SQL? (reply)

    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

  4. #4
    DBA723 Guest

    How to find last 10 records using T-SQL? (reply)

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •