0
0
PostgreSQLquery~10 mins

AGE function for differences in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the age difference between two dates using the AGE function.

PostgreSQL
SELECT AGE([1], '2023-01-01') AS difference;
Drag options to blanks, or click blank then click option'
A'2021-01-01'
B'2024-01-01'
C'2023-12-31'
D'2022-01-01'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an earlier date as the first argument results in a negative interval.
Forgetting to put the dates in quotes.
2fill in blank
medium

Complete the code to calculate the age difference between the current date and a given birthdate.

PostgreSQL
SELECT AGE(CURRENT_DATE, [1]) AS age;
Drag options to blanks, or click blank then click option'
A'1999-12-31'
B'2025-05-15'
C'2000-05-15'
D'2023-12-31'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a future date as birthdate.
Not using CURRENT_DATE for the current date.
3fill in blank
hard

Fix the error in the code to correctly calculate the age difference between two timestamps.

PostgreSQL
SELECT AGE([1], '2023-06-01 12:00:00') AS diff;
Drag options to blanks, or click blank then click option'
A'2023-05-31 12:00:00'
B'2023-06-02 12:00:00'
C'2023-06-01 11:00:00'
D'2023-06-01 13:00:00'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an earlier timestamp as the first argument causes negative intervals.
Not including time in the timestamp string.
4fill in blank
hard

Fill both blanks to calculate the age difference between two dates and filter results where the difference is more than 1 year.

PostgreSQL
SELECT AGE([1], [2]) AS age_diff FROM events WHERE AGE([1], [2]) > INTERVAL '1 year';
Drag options to blanks, or click blank then click option'
A'2020-01-01'
B'2019-12-31'
C'2021-01-01'
D'2022-01-01'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dates less than a year apart.
Swapping the dates causing negative intervals.
5fill in blank
hard

Fill all three blanks to create a query that calculates the age difference between two timestamps, extracts the year part, and filters for differences of 5 years or more.

PostgreSQL
SELECT EXTRACT(year FROM AGE([1], [2])) AS years_diff FROM users WHERE EXTRACT(year FROM AGE([1], [2])) >= [3];
Drag options to blanks, or click blank then click option'
A'2015-06-01 08:00:00'
B'2010-06-01 08:00:00'
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping timestamps causing negative intervals.
Using a string instead of a number for the threshold.
Using the wrong EXTRACT field.