0
0
PostgreSQLquery~20 mins

AGE function for differences in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AGE Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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');
A24 years 6 mons 14 days
B23 years 5 mons 14 days
C24 years 5 mons 14 days
D25 years 5 mons 14 days
Attempts:
2 left
💡 Hint
Remember AGE returns the interval between two dates as years, months, and days.
query_result
intermediate
2: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');
A33 years 5 mons 14 days
B33 years 5 mons 15 days
C34 years 5 mons 15 days
D33 years 6 mons 15 days
Attempts:
2 left
💡 Hint
Calculate the difference from 1990-12-31 to 2024-06-15.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in AGE usage
Which option contains a syntax error when using the AGE function in PostgreSQL?
ASELECT AGE('2000-01-01', '2024-06-15');
BSELECT AGE('2024-06-15', '2000-01-01');
CSELECT AGE(TIMESTAMP '2024-06-15', TIMESTAMP '2000-01-01');
DSELECT AGE('2024-06-15');
Attempts:
2 left
💡 Hint
AGE requires either one or two arguments, but one argument must be a timestamp or date.
optimization
advanced
2: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?
ASELECT EXTRACT(YEAR FROM AGE(CURRENT_DATE, birth_date)) FROM users;
BSELECT CURRENT_DATE - birth_date FROM users;
CSELECT AGE(CURRENT_DATE, birth_date) FROM users;
DSELECT DATE_PART('year', CURRENT_DATE) - DATE_PART('year', birth_date) FROM users;
Attempts:
2 left
💡 Hint
Consider which option calculates age in years correctly and efficiently.
🧠 Conceptual
expert
2:00remaining
Understanding AGE function output format
Which statement about the output of the AGE function in PostgreSQL is TRUE?
AAGE returns an interval showing years, months, and days, accounting for calendar differences.
BAGE always returns the difference as a simple integer number of days.
CAGE returns a string formatted as 'YYYY-MM-DD' representing the difference.
DAGE returns the difference in seconds as a floating-point number.
Attempts:
2 left
💡 Hint
Think about how AGE represents differences between dates.