Search code examples
sqldatetimesubquerysql-order-bysql-limit

How can I get the 10 most recent rows from a database?


I want to choose an id in a table, sort it by date and show only the 10 newest entries.

I have already tried following command:

SELECT * FROM weather 
  WHERE DATUM = (SELECT MAX(DATUM) WHERE ID='0')

Solution

  • It looks like you want filtering, sorting and limiting:

    select *
    from weather
    where id = 0          -- filter on the given "id"
    order by datum desc   -- sort by most recent date
    limit 10              -- keep the 10 most recent only