Challenge - 5 Problems
DATEDIFF and TIMESTAMPDIFF Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate1:30remaining
Calculate days difference using DATEDIFF
What is the output of this query?
SELECT DATEDIFF('2024-06-15', '2024-06-10') AS days_diff;MySQL
SELECT DATEDIFF('2024-06-15', '2024-06-10') AS days_diff;
Attempts:
2 left
💡 Hint
DATEDIFF returns the number of days between two dates as end_date - start_date.
✗ Incorrect
DATEDIFF counts how many days are between the first and second date. Here, 2024-06-15 minus 2024-06-10 equals 5 days.
❓ query_result
intermediate1:30remaining
Calculate months difference using TIMESTAMPDIFF
What is the output of this query?
SELECT TIMESTAMPDIFF(MONTH, '2023-01-01', '2024-06-01') AS months_diff;MySQL
SELECT TIMESTAMPDIFF(MONTH, '2023-01-01', '2024-06-01') AS months_diff;
Attempts:
2 left
💡 Hint
TIMESTAMPDIFF counts full months between the two dates.
✗ Incorrect
From January 1, 2023 to June 1, 2024 is 17 full months.
📝 Syntax
advanced2:00remaining
Identify the syntax error in TIMESTAMPDIFF usage
Which option contains a syntax error when using TIMESTAMPDIFF in MySQL?
Attempts:
2 left
💡 Hint
The first argument to TIMESTAMPDIFF should be a keyword, not a string.
✗ Incorrect
TIMESTAMPDIFF expects the unit (e.g., DAY, MONTH) without quotes. Option A uses quotes, causing a syntax error.
❓ query_result
advanced2:00remaining
Compare DATEDIFF and TIMESTAMPDIFF outputs
What is the output of this query?
SELECT DATEDIFF('2024-06-15 12:00:00', '2024-06-10 23:59:59') AS diff1, TIMESTAMPDIFF(DAY, '2024-06-10 23:59:59', '2024-06-15 12:00:00') AS diff2;MySQL
SELECT DATEDIFF('2024-06-15 12:00:00', '2024-06-10 23:59:59') AS diff1, TIMESTAMPDIFF(DAY, '2024-06-10 23:59:59', '2024-06-15 12:00:00') AS diff2;
Attempts:
2 left
💡 Hint
DATEDIFF counts date boundaries ignoring time, TIMESTAMPDIFF counts full days.
✗ Incorrect
DATEDIFF counts the difference in days ignoring time parts, so it returns 5. TIMESTAMPDIFF counts full 24-hour days, so it returns 4.
🧠 Conceptual
expert2:00remaining
Understanding behavior of TIMESTAMPDIFF with negative intervals
What is the output of this query?
SELECT TIMESTAMPDIFF(DAY, '2024-06-15', '2024-06-10') AS days_diff;MySQL
SELECT TIMESTAMPDIFF(DAY, '2024-06-15', '2024-06-10') AS days_diff;
Attempts:
2 left
💡 Hint
TIMESTAMPDIFF returns negative values if the second date is earlier than the first.
✗ Incorrect
TIMESTAMPDIFF returns the signed difference, so when the second date is before the first, it returns a negative number.