Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to know the query for selecting the latest 25 rows from a table. ...plzz help me to do it.
Posted
Comments
[no name] 13-Oct-12 8:40am    
"plzz" is on vacation this week. Sort the result set descending and then get the top 25.
Maciej Los 15-Oct-12 16:28pm    
My virtual 5!

SQL
create table emp(emp_id int identity,emp_name varchar(20),registerationtiming varchar(20))

Select top 25 * from emp order by registerationtiming desc 
 
Share this answer
 
First you should have a column in Tables named as (Last_Updated or Recent_Visit) in datetime datatype.
That column should fill with datetime of the current timing Eg - Getdate()
Dont convert the datetime of getdate(), because you cant get latest one.

Query Below:

Select top 25 * from MyDB..MyTable with (Nolock) order by Recent_Visit desc
 
Share this answer
 
v2
solution 1 will also give you the correct result but it will be a little slow in case of many records

here is an example

SQL
SELECT ORDERID, CUSTOMERID, OrderDate

FROM

(

SELECT ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY OrderDate DESC) AS OrderedDate,*

FROM Orders

) as ordlist

WHERE ordlist.OrderedDate <= 25
 
Share this answer
 
I assume that you are deciding latest 25 based on a date..


SQL
SELECT TOP 25* FROM TABLENAME order by DATECOLUMNNAME DESC
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900