Search code examples
sqlpostgresqlgreatest-n-per-group

How do I SELECT the oldest record in a table PostgreSQL?


I have a table with columns account_id and creation_date. The creation_date gets the current timestamp when the account is created (row is inserted) - how can I get the oldest account to return? In other words, the row which has the earliest timestamp in the table?

I've tried SELECT account_id, MIN(creation_date) FROM table but can't seem to get MIN() to work how I'd like it to.


Solution

  • Looks like this was the way to do it:

    SELECT account_id, creation_date FROM table WHERE creation_date = (SELECT MIN(creation_date) FROM table)