Bird
Raised Fist0
MySQLquery~10 mins

DATEDIFF and TIMESTAMPDIFF in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - DATEDIFF and TIMESTAMPDIFF
Start with two dates
↓
Choose function: DATEDIFF or TIMESTAMPDIFF
↓
DATEDIFF: Calculate days between dates
↓
TIMESTAMPDIFF: Choose unit (e.g., SECOND, MINUTE, DAY)
↓
Calculate difference in chosen unit
↓
Return integer result
DATEDIFF returns the number of days between two dates. TIMESTAMPDIFF returns the difference between two dates or datetimes in a specified unit.
Execution Sample
MySQL
SELECT DATEDIFF('2024-06-10', '2024-06-01') AS diff_days;
SELECT TIMESTAMPDIFF(HOUR, '2024-06-01 10:00:00', '2024-06-02 15:00:00') AS diff_hours;
Calculate days difference with DATEDIFF and hours difference with TIMESTAMPDIFF between two date/datetime values.
Execution Table
StepFunctionInput 1Input 2Unit (for TIMESTAMPDIFF)CalculationResult
1DATEDIFF'2024-06-10''2024-06-01'N/A2024-06-10 minus 2024-06-01 in days9
2TIMESTAMPDIFF'2024-06-01 10:00:00''2024-06-02 15:00:00'HOURDifference in hours between the two timestamps29
3TIMESTAMPDIFF'2024-06-01 10:00:00''2024-06-02 15:00:00'DAYDifference in days between the two timestamps1
4DATEDIFF'2024-06-01''2024-06-10'N/A2024-06-01 minus 2024-06-10 in days-9
5TIMESTAMPDIFF'2024-06-02 15:00:00''2024-06-01 10:00:00'HOURDifference in hours reversed-29
6ENDNo more calculations
💡 All differences calculated; negative results show reversed date order.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
diff_daysN/A999-9-9-9
diff_hoursN/AN/A292929-29-29
diff_days_timestampN/AN/AN/A1111
Key Moments - 3 Insights
Why does DATEDIFF return a negative number in step 4?
DATEDIFF subtracts the second date from the first. If the first date is earlier, the result is negative, as shown in step 4 where '2024-06-01' minus '2024-06-10' equals -9.
Why do TIMESTAMPDIFF results depend on the unit chosen?
TIMESTAMPDIFF calculates difference in the unit you specify (e.g., HOUR, DAY). Step 2 shows 29 hours difference, while step 3 shows 1 day difference for the same timestamps.
Can DATEDIFF be used with time values?
No, DATEDIFF only works with dates and returns difference in days. For time or smaller units, TIMESTAMPDIFF should be used, as shown in steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of DATEDIFF('2024-06-10', '2024-06-01') at step 1?
A-9
B1
C9
D29
💡 Hint
Check the 'Result' column in row for step 1 in the execution_table.
At which step does TIMESTAMPDIFF calculate the difference in days?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Unit' column in the execution_table to find where 'DAY' is used.
If the dates in step 5 were swapped, what would be the new result for TIMESTAMPDIFF in hours?
A1
B29
C-29
D-1
💡 Hint
Refer to step 2 and 5 in execution_table to see how swapping inputs changes sign.
Concept Snapshot
DATEDIFF(date1, date2) returns days between dates (date1 - date2).
TIMESTAMPDIFF(unit, datetime1, datetime2) returns difference in specified unit.
Units include SECOND, MINUTE, HOUR, DAY, MONTH, YEAR.
Negative results mean the first date/time is earlier.
Use DATEDIFF for days only; TIMESTAMPDIFF for flexible units.
Full Transcript
This visual execution shows how MySQL functions DATEDIFF and TIMESTAMPDIFF calculate differences between dates and datetimes. DATEDIFF returns the number of days between two dates by subtracting the second date from the first. TIMESTAMPDIFF allows specifying the unit of difference, such as hours or days, and calculates accordingly. Negative results occur when the first date/time is earlier than the second. The execution table traces step-by-step calculations with example inputs, showing results and how changing units or input order affects output. Variable tracking highlights how difference values change after each step. Key moments clarify common confusions about negative results, unit choices, and function limitations. The quiz tests understanding by referencing specific steps and results. The snapshot summarizes syntax and behavior for quick reference.

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