We use DATEDIFF and TIMESTAMPDIFF to find the difference between two dates or times. This helps us understand how much time passed between events.
DATEDIFF and TIMESTAMPDIFF in MySQL
DATEDIFF(date1, date2) TIMESTAMPDIFF(unit, datetime1, datetime2)
DATEDIFF returns the number of days between two dates.
TIMESTAMPDIFF lets you choose the unit like SECOND, MINUTE, HOUR, DAY, MONTH, or YEAR.
SELECT DATEDIFF('2024-06-10', '2024-06-01');
SELECT TIMESTAMPDIFF(MONTH, '2023-01-01', '2024-06-01');
SELECT TIMESTAMPDIFF(HOUR, '2024-06-10 08:00:00', '2024-06-10 15:30:00');
This query shows how to use DATEDIFF and TIMESTAMPDIFF to find differences in days, hours, and minutes between two dates or timestamps.
SELECT DATEDIFF('2024-06-15', '2024-06-10') AS days_diff, TIMESTAMPDIFF(DAY, '2024-06-10 08:00:00', '2024-06-15 20:00:00') AS days_diff_timestamp, TIMESTAMPDIFF(HOUR, '2024-06-10 08:00:00', '2024-06-10 20:00:00') AS hours_diff, TIMESTAMPDIFF(MINUTE, '2024-06-10 08:00:00', '2024-06-10 08:30:00') AS minutes_diff;
DATEDIFF only counts full days and ignores time parts.
TIMESTAMPDIFF counts full units (like full hours or full minutes) between two timestamps.
For positive results, use DATEDIFF(later, earlier) or TIMESTAMPDIFF(unit, earlier, later).
Use DATEDIFF to find days between two dates.
Use TIMESTAMPDIFF to find difference in various units like seconds, minutes, hours, days, months, or years.
Both help measure time passed between events easily.
Practice
DATEDIFF(date1, date2) return?Solution
Step 1: Understand the function purpose
DATEDIFFcalculates the difference between two dates in days only.Step 2: Confirm the output type
The output is an integer representing how many days separate the two dates.Final Answer:
The number of days between date1 and date2 -> Option AQuick Check:
DATEDIFF returns days difference [OK]
- Thinking it returns seconds or months
- Confusing order of dates for output sign
- Using it to get current date/time
start_time and end_time using TIMESTAMPDIFF?Solution
Step 1: Recall TIMESTAMPDIFF syntax
The correct syntax isTIMESTAMPDIFF(unit, datetime1, datetime2)where unit is a keyword like HOUR without quotes.Step 2: Match parameters order
Unit first, then start_time, then end_time is correct.Final Answer:
TIMESTAMPDIFF(HOUR, start_time, end_time) -> Option AQuick Check:
Unit first, then start, then end [OK]
- Putting unit last or in quotes
- Swapping start and end parameters
- Using lowercase unit in quotes
SELECT TIMESTAMPDIFF(DAY, '2024-01-01', '2024-01-10') AS diff_days;What will be the output of
diff_days?Solution
Step 1: Understand TIMESTAMPDIFF with DAY unit
It counts full days between the two dates, excluding the start date.Step 2: Calculate days between 2024-01-01 and 2024-01-10
From Jan 1 to Jan 10 is 9 full days difference.Final Answer:
9 -> Option DQuick Check:
10 - 1 = 9 days difference [OK]
- Counting both start and end dates
- Expecting 10 instead of 9
- Confusing with DATEDIFF behavior
SELECT TIMESTAMPDIFF('MINUTE', '2024-04-01 10:00:00', '2024-04-01 11:30:00');Solution
Step 1: Check TIMESTAMPDIFF unit syntax
The unit keyword like MINUTE must be unquoted, as a keyword, not a string.Step 2: Validate other parts
The dates and order are correct; MINUTE is a valid unit; datetime format is allowed.Final Answer:
Unit 'MINUTE' should not be in quotes -> Option CQuick Check:
Unit keywords are unquoted [OK]
- Quoting the unit keyword
- Swapping date order unnecessarily
- Thinking datetime is invalid input
'2023-01-15' and '2024-06-10', but only count full months passed. Which query correctly does this?Solution
Step 1: Understand requirement for full months
We need full months difference, not approximate days divided by 30.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.Final Answer:
SELECT TIMESTAMPDIFF(MONTH, '2023-01-15', '2024-06-10'); -> Option BQuick Check:
Use TIMESTAMPDIFF with MONTH for full months [OK]
- Dividing days by 30 for months
- Using YEAR unit and multiplying
- Using DATEDIFF which returns days only
Ready to prove it?
Pass the test and this pattern moves unknown
