0
0
PostgreSQLquery~10 mins

AGE function for differences in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - AGE function for differences
Input: two dates
Calculate difference
Return interval result
Output age as years, months, days
The AGE function takes two dates and calculates the difference between them, returning the result as an interval showing years, months, and days.
Execution Sample
PostgreSQL
SELECT AGE('2024-06-15', '2020-01-10');
Calculates the difference between June 15, 2024 and January 10, 2020 as an interval.
Execution Table
StepInput DatesActionIntermediate ResultOutput
1'2024-06-15', '2020-01-10'Parse datesDates recognizedN/A
2Dates parsedCalculate difference4 years, 5 months, 5 daysN/A
3Difference calculatedFormat as intervalInterval type4 years 5 mons 5 days
4Result formattedReturn outputFinal interval4 years 5 mons 5 days
💡 Completed calculation and returned interval representing age difference.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
date1N/A2024-06-152024-06-152024-06-152024-06-15
date2N/A2020-01-102020-01-102020-01-102020-01-10
differenceN/AN/A4 years 5 mons 5 days4 years 5 mons 5 days4 years 5 mons 5 days
Key Moments - 2 Insights
Why does AGE return an interval instead of just a number?
AGE returns an interval to show the difference in years, months, and days clearly, as seen in execution_table step 3 where the result is formatted as an interval.
What happens if the first date is earlier than the second date?
AGE will return a negative interval showing the difference in reverse order, but the calculation steps remain the same as shown in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 3?
ADates recognized
B4 years 5 mons 5 days
CFinal interval
DN/A
💡 Hint
Check the 'Output' column in row for step 3 in execution_table.
At which step is the difference between the two dates calculated?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when calculation happens.
If date1 was '2019-01-10' and date2 was '2020-06-15', how would the difference appear?
A-1 year 5 mons 5 days
B1 year 5 mons 5 days
C4 years 5 mons 5 days
D-4 years 5 mons 5 days
💡 Hint
AGE returns negative interval if first date is earlier, check variable_tracker for date order.
Concept Snapshot
AGE(date1, date2) returns the interval difference between two dates.
Result shows years, months, and days.
If date1 is earlier, result is negative interval.
Useful for calculating age or time spans.
Returns type: interval.
Full Transcript
The AGE function in PostgreSQL calculates the difference between two dates and returns it as an interval showing years, months, and days. It first parses the input dates, then calculates the difference, formats it as an interval, and finally returns the result. The output helps understand the exact time span between two dates, useful for age calculations. If the first date is earlier than the second, the result is a negative interval. This step-by-step execution shows how the function processes inputs and produces the output interval.