0
0
MySQLquery~5 mins

DATEDIFF and TIMESTAMPDIFF in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the DATEDIFF function do in MySQL?

DATEDIFF returns the number of days between two date values.

It subtracts the second date from the first and gives the difference in days.

Click to reveal answer
beginner
How is TIMESTAMPDIFF different from DATEDIFF?

TIMESTAMPDIFF can return the difference between two dates in various units like seconds, minutes, hours, days, months, or years.

DATEDIFF only returns the difference in days.

Click to reveal answer
beginner
Write a MySQL query using DATEDIFF to find how many days have passed since '2024-01-01'.
SELECT DATEDIFF(CURDATE(), '2024-01-01') AS days_passed;

This returns the number of days from January 1, 2024, to today.

Click to reveal answer
intermediate
What is the syntax of TIMESTAMPDIFF in MySQL?
TIMESTAMPDIFF(unit, datetime_expr1, datetime_expr2)

Where unit can be SECOND, MINUTE, HOUR, DAY, MONTH, YEAR, etc.

It returns the difference between datetime_expr2 and datetime_expr1 in the specified unit.

Click to reveal answer
beginner
If you want to find the number of months between two dates, which function and unit would you use?

You would use TIMESTAMPDIFF with the unit MONTH.

SELECT TIMESTAMPDIFF(MONTH, '2023-01-01', '2024-06-01');
Click to reveal answer
What does DATEDIFF('2024-06-10', '2024-06-01') return?
A9
B10
C1
D0
Which function can return the difference in hours between two timestamps?
ADATEDIFF
BNOW
CTIMESTAMPDIFF
DDATE_ADD
What unit would you use with TIMESTAMPDIFF to get the difference in minutes?
AMINUTE
BMONTH
CSECOND
DHOUR
What will DATEDIFF('2024-06-01', '2024-06-10') return?
A9
B0
C10
D-9
Which of these is a valid unit for TIMESTAMPDIFF?
AWEEK
BHOUR
CDECADE
DCENTURY
Explain how to use DATEDIFF and what kind of result it returns.
Think about how many days passed between two dates.
You got /3 concepts.
    Describe the flexibility of TIMESTAMPDIFF compared to DATEDIFF.
    Consider what units you might want to measure time differences in.
    You got /3 concepts.