how to find the last 10 row in a table in sql server
Question
Wednesday, August 15, 2012 1:39 PM
Hi
Regards
Chandrasekhar
All replies (3)
Wednesday, August 15, 2012 1:53 PM ✅Answered
SELECT TOP 10 *
FROM MyTable
ORDER BY SomeOrderingColumn DESC
Assuming that:
- MyTable is the name of your table
- SomeOrderingColumn is the name of the column by which you determine the "last" rows
- DESC which means Descending, meaning that the highest value is considered the "last".
- If it's the other way around (the lowest value is considered the "last") then replace DESC with ASC
Eitan Blumin; SQL Server Consultant - Madeira SQL Server Services; http://www.madeira.co.il/author/eitan/
Wednesday, August 15, 2012 2:21 PM ✅Answered
Hi Chandrasekhar,
When you say :last 10 row in a table" , you need to first define the criteria for this ordering. Do you want the latest 10 rows inserting into a table by time ? If Yes, do you have a column in that table to store the date & time when a row was inserted into a table.
One you have defined the "order" criteria - you can use the queries proposed in Eitan's reply as well as Seth's Reply.
I would strongly recommend for you to add more details when you post a question here, as these details help us give you a better answer.
Sanil Mhatre | Database Developer | MCTS | If you find my reply useful in any way, please vote it as helpful. If it has helped answer your question, please mark it as Answer. http://sqlwithsanil.com
Wednesday, August 15, 2012 1:53 PM
Hi
Select top 10 *
FROM YourTable
Order by ColumnName desc
You need to choose which column you want to order by and then order it descending.
Seth