Bird
Raised Fist0
MySQLquery~5 mins

DATEDIFF and TIMESTAMPDIFF in MySQL - Cheat Sheet & Quick Revision

Choose your learning style10 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.

      Practice

      (1/5)
      1. What does the MySQL function DATEDIFF(date1, date2) return?
      easy
      A. The number of days between date1 and date2
      B. The number of seconds between date1 and date2
      C. The difference in months between date1 and date2
      D. The current date and time

      Solution

      1. Step 1: Understand the function purpose

        DATEDIFF calculates the difference between two dates in days only.
      2. Step 2: Confirm the output type

        The output is an integer representing how many days separate the two dates.
      3. Final Answer:

        The number of days between date1 and date2 -> Option A
      4. Quick Check:

        DATEDIFF returns days difference [OK]
      Hint: DATEDIFF always returns days difference between two dates [OK]
      Common Mistakes:
      • Thinking it returns seconds or months
      • Confusing order of dates for output sign
      • Using it to get current date/time
      2. Which of the following is the correct syntax to find the difference in hours between two datetime values start_time and end_time using TIMESTAMPDIFF?
      easy
      A. TIMESTAMPDIFF(HOUR, start_time, end_time)
      B. TIMESTAMPDIFF(start_time, end_time, HOUR)
      C. TIMESTAMPDIFF('HOUR', end_time, start_time)
      D. TIMESTAMPDIFF(end_time, start_time, 'HOUR')

      Solution

      1. Step 1: Recall TIMESTAMPDIFF syntax

        The correct syntax is TIMESTAMPDIFF(unit, datetime1, datetime2) where unit is a keyword like HOUR without quotes.
      2. Step 2: Match parameters order

        Unit first, then start_time, then end_time is correct.
      3. Final Answer:

        TIMESTAMPDIFF(HOUR, start_time, end_time) -> Option A
      4. Quick Check:

        Unit first, then start, then end [OK]
      Hint: TIMESTAMPDIFF(unit, start, end) with unit unquoted [OK]
      Common Mistakes:
      • Putting unit last or in quotes
      • Swapping start and end parameters
      • Using lowercase unit in quotes
      3. Given the query:
      SELECT TIMESTAMPDIFF(DAY, '2024-01-01', '2024-01-10') AS diff_days;
      What will be the output of diff_days?
      medium
      A. Error
      B. 10
      C. 11
      D. 9

      Solution

      1. Step 1: Understand TIMESTAMPDIFF with DAY unit

        It counts full days between the two dates, excluding the start date.
      2. Step 2: Calculate days between 2024-01-01 and 2024-01-10

        From Jan 1 to Jan 10 is 9 full days difference.
      3. Final Answer:

        9 -> Option D
      4. Quick Check:

        10 - 1 = 9 days difference [OK]
      Hint: TIMESTAMPDIFF counts full units between dates [OK]
      Common Mistakes:
      • Counting both start and end dates
      • Expecting 10 instead of 9
      • Confusing with DATEDIFF behavior
      4. Identify the error in this query:
      SELECT TIMESTAMPDIFF('MINUTE', '2024-04-01 10:00:00', '2024-04-01 11:30:00');
      medium
      A. TIMESTAMPDIFF cannot use MINUTE as unit
      B. Dates must be swapped in order
      C. Unit 'MINUTE' should not be in quotes
      D. Dates must be in DATE format, not datetime

      Solution

      1. Step 1: Check TIMESTAMPDIFF unit syntax

        The unit keyword like MINUTE must be unquoted, as a keyword, not a string.
      2. Step 2: Validate other parts

        The dates and order are correct; MINUTE is a valid unit; datetime format is allowed.
      3. Final Answer:

        Unit 'MINUTE' should not be in quotes -> Option C
      4. Quick Check:

        Unit keywords are unquoted [OK]
      Hint: Remove quotes around unit in TIMESTAMPDIFF [OK]
      Common Mistakes:
      • Quoting the unit keyword
      • Swapping date order unnecessarily
      • Thinking datetime is invalid input
      5. You want to find the total number of months between two dates '2023-01-15' and '2024-06-10', but only count full months passed. Which query correctly does this?
      hard
      A. SELECT DATEDIFF('2024-06-10', '2023-01-15') / 30;
      B. SELECT TIMESTAMPDIFF(MONTH, '2023-01-15', '2024-06-10');
      C. SELECT TIMESTAMPDIFF(DAY, '2023-01-15', '2024-06-10') / 30;
      D. SELECT TIMESTAMPDIFF(YEAR, '2023-01-15', '2024-06-10') * 12;

      Solution

      1. Step 1: Understand requirement for full months

        We need full months difference, not approximate days divided by 30.
      2. Step 2: Check each option

        SELECT TIMESTAMPDIFF(MONTH, '2023-01-15', '2024-06-10'); uses TIMESTAMPDIFF with MONTH unit, which counts full months correctly.
        SELECT DATEDIFF('2024-06-10', '2023-01-15') / 30; and C use days divided by 30, which is approximate and can be wrong.
        SELECT TIMESTAMPDIFF(YEAR, '2023-01-15', '2024-06-10') * 12; counts years times 12, ignoring leftover months.
      3. Final Answer:

        SELECT TIMESTAMPDIFF(MONTH, '2023-01-15', '2024-06-10'); -> Option B
      4. Quick Check:

        Use TIMESTAMPDIFF with MONTH for full months [OK]
      Hint: Use TIMESTAMPDIFF with MONTH for full months difference [OK]
      Common Mistakes:
      • Dividing days by 30 for months
      • Using YEAR unit and multiplying
      • Using DATEDIFF which returns days only
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard