tags:

views:

71

answers:

1

i have a table that has event name and event date.

if i want to get the last 10 events by date, can i do that by sql or do i need query the whole table and do the filtering in code.

+3  A: 

This assumes that "the last 10 by event date" means the 10 most recent ones:

SELECT TOP 10 EventName, EventDate
FROM EventsTable
ORDER BY EventDate DESC

That should do it - regardless of what database system you're using.

Marc

marc_s
"regardless of what database system you're using" is not true. ;-) MySQL differs a little and Oracle differs quite a lot.
Tomalak
at least 5 different ways to answer this question http://stackoverflow.com/questions/595123/is-there-an-ansi-sql-alternative-to-the-mysql-limit-keyword
Sam Saffron
"regardless of what database system you're using" as long as you are using Microsoft SQL Server or Sybase. This is like the line in The Blue Brothers: "Oh, we got both kinds (of music). We got country *and* western."
Bill Karwin
@BIll: like it :-) Seriously - is even this very basic SQL totally different in MySQL, PostgreSQL, Oracle?? Sheesh......
marc_s