0

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.

leora
  • 188,729
  • 360
  • 878
  • 1,366

1 Answers1

4

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
  • 732,580
  • 175
  • 1,330
  • 1,459
  • "regardless of what database system you're using" is not true. ;-) MySQL differs a little and Oracle differs quite a lot. – Tomalak Sep 05 '09 at 12:59
  • 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 Sep 05 '09 at 13:00
  • 1
    "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 Sep 05 '09 at 17:36
  • @BIll: like it :-) Seriously - is even this very basic SQL totally different in MySQL, PostgreSQL, Oracle?? Sheesh...... – marc_s Sep 06 '09 at 07:59