Challenge - 5 Problems
Date Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate date after adding interval
What is the result of this query?
SELECT DATE '2024-01-15' + INTERVAL '10 days';
PostgreSQL
SELECT DATE '2024-01-15' + INTERVAL '10 days';
Attempts:
2 left
💡 Hint
Adding an interval to a date returns a timestamp with time set to midnight.
✗ Incorrect
Adding an interval to a DATE type in PostgreSQL returns a TIMESTAMP. The date '2024-01-15' plus 10 days is '2024-01-25 00:00:00'.
❓ query_result
intermediate2:00remaining
Subtract interval from timestamp
What is the output of this query?
SELECT TIMESTAMP '2024-06-01 15:30:00' - INTERVAL '3 hours 15 minutes';
PostgreSQL
SELECT TIMESTAMP '2024-06-01 15:30:00' - INTERVAL '3 hours 15 minutes';
Attempts:
2 left
💡 Hint
Subtracting an interval reduces the timestamp by that amount.
✗ Incorrect
Subtracting 3 hours and 15 minutes from 15:30 results in 12:15 on the same day.
📝 Syntax
advanced2:00remaining
Identify the syntax error in interval addition
Which option will cause a syntax error when adding an interval to a date in PostgreSQL?
Attempts:
2 left
💡 Hint
Intervals must be explicitly cast or written with INTERVAL keyword.
✗ Incorrect
Option A tries to add a string directly to a date without casting it as an interval, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimize interval addition for multiple rows
Given a table events(event_date DATE), which query is more efficient to add 7 days to all event_date values?
Attempts:
2 left
💡 Hint
Using standard interval units can help the query planner optimize better.
✗ Incorrect
INTERVAL '1 week' is a standard interval unit and is more readable and potentially optimized than specifying days or hours explicitly.
🧠 Conceptual
expert2:00remaining
Understanding interval multiplication and addition
What is the output of this query?
SELECT DATE '2024-01-01' + INTERVAL '1 day' * 3;
PostgreSQL
SELECT DATE '2024-01-01' + INTERVAL '1 day' * 3;
Attempts:
2 left
💡 Hint
Multiplying an interval by an integer repeats the interval that many times.
✗ Incorrect
INTERVAL '1 day' * 3 equals 3 days. Adding 3 days to 2024-01-01 results in 2024-01-04 00:00:00. PostgreSQL allows multiplying intervals by integers, so the expression is valid and returns 2024-01-04 00:00:00.