SQL Help

  • Thread starter Thread starter Wee
  • Start date Start date

Wee

Wee

Associate
Joined
25 Apr 2006
Posts
130
Location
Scotland
Working on some web stuff at the moment, and have come across a problem. I have a table in my database that is choco-block full of rows, but only want to display the 5 latest entries instead of them all. I was wondering it there is an SQL statement that is able to only output a specific number of rows?
 
yes.

How are they indexed? if they are by a incrementing key number then order by that and get the last 5 records.

what columns are in the table and what database engine is it?
 
Need to know what database, Oracle is quite different from most of the others with respect to getting the first n of a query
 
The database is SQL Server 2005.

They won't be indexed by an incremented key number, most likely will be done by date.
 
I'd have thought it'd be a SELECT TOP 5 * FROM <TABLENAME> ORDER BY <COLUMN NAME> DESC if it's MS SQL Server :)

arty
 
arty said:
I'd have thought it'd be a SELECT TOP 5 * FROM <TABLENAME> ORDER BY <COLUMN NAME> DESC if it's MS SQL Server :)

arty

Both work. MySql doesn't like the "TOP" bit though.
 
.....But yeah use his. It is more 'correct' than mine (even though both will work). I don't have a MSSQL server to test it on at home though!
 
arty said:
I'd have thought it'd be a SELECT TOP 5 * FROM <TABLENAME> ORDER BY <COLUMN NAME> DESC if it's MS SQL Server :)

arty

Appreciate the help folks, it worked a treat!
 
just be aware that the TOP 5 syntax is not part of standard SQL so if you ever come to port your app to another database engine this statement will have ti change ;)

HT
 
Back
Top Bottom