PostgreSQL - AGE Function Last Updated : 01 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In PostgreSQL, the AGE() function is a powerful tool for calculating the difference between two TIMESTAMP values. This function is especially useful for determining the age of individuals or the duration between events. Let us better understand the AGE() Function in PostgreSQL from this article.Syntaxage(timestamp, timestamp);Let's analyze the above syntax:Parameters: The function takes two arguments, both of which are TIMESTAMP values.Operation: It subtracts the second TIMESTAMP from the first one, resulting in an interval that represents the difference between the two timestamps.PostgreSQL AGE Function ExamplesLet us take a look at some of the examples of AGE Function in PostgreSQL to better understand the concept.Example 1: Calculating Age from a BirthdateHere 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.Query:SELECT current_date, AGE(timestamp '2000-01-01');Output:Explanation: This query will return the current date and the calculated age of the individual based on the birthdate provided. The AGE() function will return an interval showing the number of years, months, and days.Example 2: Determining Rental DurationsThe below statement query to get the top 10 rentals that have the longest durations, from the 'rental' table of the sample database:Query:SELECT rental_id, customer_id, age(return_date, rental_date) AS durationFROM rentalWHERE return_date IS NOT NULLORDER BY duration DESC LIMIT 10;Output:Explanation: This query retrieves the rental ID, customer ID, and the calculated duration for each rental where the return date is available. The results are ordered by duration in descending order, showing the top 10 rentals with the longest durations.Important Points About PostgreSQL AGE FunctionIf only one TIMESTAMP argument is provided, age() calculates the interval from the provided timestamp to the current date and time.If either of the TIMESTAMP values is NULL, the result will be NULL. age() works with TIMESTAMP, DATE, and INTERVAL data types. When using DATE values, they are implicitly cast to TIMESTAMP values with a time component of '00:00:00'.The interval result is approximate when dealing with complex calendar calculations involving leap years or varying month lengths. Comment More infoAdvertise with us R RajuKumar19 Follow Improve Article Tags : PostgreSQL PostgreSQL-function PostgreSQL-Date-function Similar Reads PostgreSQL Tutorial In this PostgreSQL tutorial youâll learn the basic data types(Boolean, char, text, time, int etc.), Querying and Filtering techniques like select, where, in, order by, etc. managing and modifying the tables in PostgreSQL. Weâll cover all the basic to advance concepts of PostgreSQL in this tutorial. 8 min read PostgreSQL DATEDIFF Function PostgreSQL doesnât have a DATEDIFF function like some other databases, but you can still calculate the difference between dates using simple subtraction. This approach allows you to find out how many days, months, or years separate two dates. In this article, we'll explore how to compute date differ 6 min read PostgreSQL - Data Types PostgreSQL is a powerful, open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column. which allows developers to define, store, and manipulate data in a way that aligns w 5 min read PostgreSQL - Psql commands PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively.Here, we highlight some of th 2 min read Top 50 PostgreSQL Interview Questions and Answers Are you preparing for a PostgreSQL interview? PostgreSQL is a powerful open-source relational database management system (RDBMS) that is well-known for its reliability, scalability, and rich set of features. Itâs a favorite among developers and businesses alike, making it essential to master if we w 15+ min read PostgreSQL - Create Database Creating a database in PostgreSQL is an important task for developers and database administrators to manage data effectively. PostgreSQL provides multiple ways to create a database, catering to different user preferences, whether through the command-line interface or using a graphical interface like 5 min read How to Dump and Restore PostgreSQL Database? PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases betw 6 min read PostgreSQL - SERIAL When working with PostgreSQL, we need to create tables with unique primary keys. PostgreSQL offers a powerful feature known as the SERIAL pseudo-type which simplifies generating auto-incrementing sequences for columns. In this article, weâll learn about the PostgreSQL SERIAL pseudo-type by explain h 5 min read PostgreSQL - DISTINCT ON expression The DISTINCT ON clause in PostgreSQL allows us to retrieve unique rows based on specific columns by offering more flexibility than the standard DISTINCT clause. DISTINCT ON allow us to specify which row to keep for each unique value based on an ORDER BY clause. This is particularly useful for select 5 min read PostgreSQL Connection String A connection string is an essential component that enables applications to communicate with databases or other data sources by providing the necessary configuration details. It consolidates critical information such as the server address, database name, user credentials, and additional parameters li 4 min read Like