Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL
I want to know the query for selecting the latest 25 rows from a table. ...plzz help me to do it.
Posted 13-Oct-12 3:36am
Comments
Wes Aday at 13-Oct-12 8:40am
   
"plzz" is on vacation this week. Sort the result set descending and then get the top 25.
Maciej Los at 15-Oct-12 16:28pm
   
My virtual 5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I assume that you are deciding latest 25 based on a date..
 

SELECT TOP 25* FROM TABLENAME order by DATECOLUMNNAME DESC
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

create table emp(emp_id int identity,emp_name varchar(20),registerationtiming varchar(20))
 
Select top 25 * from emp order by registerationtiming desc 
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 485
1 OriginalGriff 310
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 200
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 13 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100