Postgresql Querying the last 10 records from the database

Hi Sir,
Can you help me in writing a query for retrieving Postgresql Querying the last 10 records (recent) from the database. Its urgent
ekarthaAsked:
Who is Participating?
 
ldbkuttyCommented:
If you just want to retrieve the last 10 records which were inserted (not considering update),      with your auto_increment column, you can use:
SELECT * FROM yourtablename ORDER BY your_auto_increment_column LIMIT 10;
      
PS: ORDER BY your_auto_increment_column is optional. This should be enough:
SELECT * FROM yourtablename LIMIT 10;
      
However, if the records are updatable and you want to retrieve the last 10 records which were accessed (either updated or inserted records), you need a date column like 'last_modified' which stores the date & time at which a record was inserted or updated.
      
Then you can use:
SELECT * FROM yourtablename ORDER BY last_modified_column LIMIT 10;
0
 
rjkimbleCommented:
I think you need to add "DESCENDING" to those ORDER BY clauses to achieve what you want.
0
 
ldbkuttyCommented:
yes, good point. So, the query should be like:
SELECT * FROM yourtablename ORDER BY corresponding_col DESC LIMIT 10;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.