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 →Example
How long ago something happened
SELECT name, AGE(CURRENT_DATE, signup_date) AS tenure FROM customers| name | tenure |
|---|---|
| Alex Park | 3 years 3 months 1 day |
| Jordan Lee | 1 year 9 months 16 days |
| Sam Rivera | 5 months 27 days |
Step by step
- Run the query without AGE so you can see what the unfiltered values look like.
SELECT name, CURRENT_DATE FROM customers; - AGE returns the difference between two dates as an interval (years/months/days).
SELECT name, AGE(CURRENT_DATE, signup_date) FROM customers; - 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
1. Using orders, write a query that calculates the age (years, months, days) since each order_date.
SELECT AGE(order_date) FROM orders2. Using shipments, write a query that calculates the age (years, months, days) since each ship_date.
SELECT AGE(ship_date) FROM shipments3. Using promotions, write a query that calculates the age (years, months, days) since each start_date.
SELECT AGE(start_date) FROM promotions4. Using store_sales, write a query that calculates the age (years, months, days) since each sale_date.
SELECT AGE(sale_date) FROM store_sales5. Using returns, write a query that calculates the age (years, months, days) since each return_date.
SELECT AGE(return_date) FROM returnsLearn 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 →