Open In App

PostgreSQL – AGE Function

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

Syntax: age(timestamp, timestamp);

Let’s analyze the above syntax:



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:

Article Tags :