0
0
MySQLquery~20 mins

DATEDIFF and TIMESTAMPDIFF in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DATEDIFF and TIMESTAMPDIFF Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
1: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;
A5
B6
C-5
D10
Attempts:
2 left
💡 Hint
DATEDIFF returns the number of days between two dates as end_date - start_date.
query_result
intermediate
1: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;
A19
B18
C17
D16
Attempts:
2 left
💡 Hint
TIMESTAMPDIFF counts full months between the two dates.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in TIMESTAMPDIFF usage
Which option contains a syntax error when using TIMESTAMPDIFF in MySQL?
ASELECT TIMESTAMPDIFF('DAY', '2024-01-01', '2024-01-10');
BSELECT TIMESTAMPDIFF(MONTH, '2024-01-01', '2024-02-01');
CSELECT TIMESTAMPDIFF(DAY, '2024-01-01', '2024-01-10');
DSELECT TIMESTAMPDIFF(YEAR, '2023-01-01', '2024-01-01');
Attempts:
2 left
💡 Hint
The first argument to TIMESTAMPDIFF should be a keyword, not a string.
query_result
advanced
2: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;
Adiff1 = 4, diff2 = 4
Bdiff1 = 4, diff2 = 5
Cdiff1 = 5, diff2 = 5
Ddiff1 = 5, diff2 = 4
Attempts:
2 left
💡 Hint
DATEDIFF counts date boundaries ignoring time, TIMESTAMPDIFF counts full days.
🧠 Conceptual
expert
2: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;
A5
B-5
C0
DNULL
Attempts:
2 left
💡 Hint
TIMESTAMPDIFF returns negative values if the second date is earlier than the first.