BBampaa

Lessons › SQL › Deep Dive: Text, Date & Number Functions

SQL lesson

AGE() for date intervals

AGE() converts the gap between two dates into a human-readable breakdown of years, months, and days instead of one raw number.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

How long ago something happened

SELECT name, AGE(CURRENT_DATE, signup_date) AS tenure FROM customers

Result:

nametenure
Alex Park3 years 3 months 1 day
Jordan Lee1 year 9 months 16 days
Sam Rivera5 months 27 days

Step by step

  1. Run the query without AGE so you can see what the unfiltered values look like.
    SELECT name, CURRENT_DATE FROM customers;
  2. AGE returns the difference between two dates as an interval (years/months/days).
    SELECT name, AGE(CURRENT_DATE, signup_date) FROM customers;
  3. AS gives the AGE result a friendly column name so the output reads cleanly.
    SELECT name, AGE(CURRENT_DATE, signup_date) AS tenure FROM customers;

Practice

Try each one in your head first, then open the answer.

1. Using orders, write a query that calculates the age (years, months, days) since each order_date.
SELECT AGE(order_date) FROM orders
2. Using shipments, write a query that calculates the age (years, months, days) since each ship_date.
SELECT AGE(ship_date) FROM shipments
3. Using promotions, write a query that calculates the age (years, months, days) since each start_date.
SELECT AGE(start_date) FROM promotions
4. Using store_sales, write a query that calculates the age (years, months, days) since each sale_date.
SELECT AGE(sale_date) FROM store_sales
5. Using returns, write a query that calculates the age (years, months, days) since each return_date.
SELECT AGE(return_date) FROM returns

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed