Challenge - 5 Problems
AGE Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate age difference between two dates
What is the output of this query in PostgreSQL?
SELECT AGE('2024-06-15', '2000-01-01');PostgreSQL
SELECT AGE('2024-06-15', '2000-01-01');
Attempts:
2 left
💡 Hint
Remember AGE returns the interval between two dates as years, months, and days.
✗ Incorrect
The AGE function calculates the difference between the first and second date. From 2000-01-01 to 2024-06-15 is 24 years, 5 months, and 14 days.
❓ query_result
intermediate2:00remaining
AGE function with current date
What will this query return if today is 2024-06-15?
SELECT AGE(CURRENT_DATE, '1990-12-31');PostgreSQL
SELECT AGE(CURRENT_DATE, '1990-12-31');
Attempts:
2 left
💡 Hint
Calculate the difference from 1990-12-31 to 2024-06-15.
✗ Incorrect
From 1990-12-31 to 2024-06-15 is 33 years, 5 months, and 15 days.
📝 Syntax
advanced2:00remaining
Identify the syntax error in AGE usage
Which option contains a syntax error when using the AGE function in PostgreSQL?
Attempts:
2 left
💡 Hint
AGE requires either one or two arguments, but one argument must be a timestamp or date.
✗ Incorrect
AGE with one argument returns the interval between that date and the current date. AGE with two arguments returns the interval between them. Option D is missing the second argument and uses a string literal without casting, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimize AGE function usage for large datasets
You want to calculate the age difference between a column 'birth_date' and today for millions of rows efficiently. Which query is best optimized?
Attempts:
2 left
💡 Hint
Consider which option calculates age in years correctly and efficiently.
✗ Incorrect
Option A uses AGE and extracts the year part, giving accurate age in years. Option A returns interval which is less useful directly. Option A returns days difference, not age. Option A only subtracts years ignoring months and days, causing inaccuracies.
🧠 Conceptual
expert2:00remaining
Understanding AGE function output format
Which statement about the output of the AGE function in PostgreSQL is TRUE?
Attempts:
2 left
💡 Hint
Think about how AGE represents differences between dates.
✗ Incorrect
AGE returns an interval type that shows the difference in years, months, and days, considering calendar variations like leap years and varying month lengths.