Skip to content
Related Articles

Related Articles

PostgreSQL – AGE Function
  • Last Updated : 28 Aug, 2020

In PostgreSQL the age() function is used to calculate ages.

Syntax: age(timestamp, timestamp);

Let’s analyze the above syntax:

  • The age() function accepts two TIMESTAMP values
  • It subtracts the second argument from the first one and returns an interval as a result.

Example 1:

Here we will evaluate the age of a person whose birth date is 2000-01-01 and the current date 2020-03-20, through the below statement:

SELECT current_date, 
       AGE(timestamp '2000-01-01');

Output:



Example 2:

The below statement query to get the top 10 rentals that have the longest durations, from the rental table of the sample database:

SELECT rental_id,
         customer_id,
         age(return_date,
             rental_date) AS duration
FROM rental
WHERE return_date IS NOT NULL
ORDER BY  duration DESC 
LIMIT 10;

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :