Bird
Raised Fist0
MySQLquery~20 mins

DATEDIFF and TIMESTAMPDIFF in MySQL - Practice Problems & Coding Challenges

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

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