Click here to Skip to main content
12,635,398 members (24,890 online)
Rate this:
 
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 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!
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 13 Oct 2012
Copyright © CodeProject, 1999-2016
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