Selecting bottom 10 results

Jonathan Brotto 1,076 Reputation points
2022-07-13T18:47:33.247+00:00

If I recall there is no bottom command in SQL server only top. If I want the bottom 10 results I need to sort ascending order or use that new offset command. Would that be correct?

Developer technologies Transact-SQL
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 26,586 Reputation points
    2022-07-13T19:10:52.667+00:00

    Hi @Jonathan Brotto ,

    Check it out.

    DECLARE @tbl TABLE (ID INT);  
    INSERT @tbl (ID) VALUES  
    (1),(2),(3),(4),(5);  
      
    -- select top 2  
    SELECT TOP(2) *   
    FROM @tbl  
    ORDER BY ID ASC;  
      
    -- select last 2  
    SELECT TOP(2) *   
    FROM @tbl  
    ORDER BY ID DESC;  
    

    220516-top-vs-bottom.png

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Vicky Kumar (Mindtree Consulting PVT LTD) 1,161 Reputation points Microsoft Employee
    2022-07-13T19:03:06.963+00:00

    Hi ,
    Thanks for reaching out, yes, you have to use subquery to get the top value and short it to DESC and and then change it to ASC order (This is one way of doing, you can write your own suitable query)

    SELECT
    columns
    FROM
    (
    SELECT TOP 10
    columns
    FROM
    My_Table
    ORDER BY
    column_name DESC
    ) SQ
    ORDER BY
    column_name ASC

    Hope this helps,

    thanks

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.