Bird
Raised Fist0
MySQLquery~10 mins

DATEDIFF and TIMESTAMPDIFF in MySQL - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the number of days between two dates.

MySQL
SELECT DATEDIFF([1], '2024-01-01') AS days_difference;
Drag options to blanks, or click blank then click option'
A'2024-01-10'
B'2023-12-31'
C'2024-02-01'
D'2024-01-01'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the earlier date as the first argument results in a negative number.
Forgetting to put dates in quotes.
2fill in blank
medium

Complete the code to find the number of months between two dates using TIMESTAMPDIFF.

MySQL
SELECT TIMESTAMPDIFF([1], '2023-01-01', '2024-01-01') AS months_difference;
Drag options to blanks, or click blank then click option'
ADAY
BYEAR
CMONTH
DHOUR
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'DAY' instead of 'MONTH' returns days, not months.
Using lowercase units which may cause errors.
3fill in blank
hard

Fix the error in the code to correctly calculate the difference in hours between two timestamps.

MySQL
SELECT TIMESTAMPDIFF([1], '2024-01-01 08:00:00', '2024-01-01 12:00:00') AS hours_difference;
Drag options to blanks, or click blank then click option'
ADAY
BMINUTE
CSECOND
DHOUR
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'MINUTE' returns minutes, not hours.
Using 'DAY' returns days, which is too large for this difference.
4fill in blank
hard

Fill both blanks to calculate the number of years and days between two dates.

MySQL
SELECT TIMESTAMPDIFF([1], '2000-01-01', '2024-01-01') AS years_diff, DATEDIFF('2024-01-01', [2]) AS days_diff;
Drag options to blanks, or click blank then click option'
AYEAR
B'2000-01-01'
C'2023-12-31'
DMONTH
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'MONTH' instead of 'YEAR' for years difference.
Swapping dates in DATEDIFF causing negative results.
5fill in blank
hard

Fill all three blanks to calculate the difference in days, months, and years between two dates.

MySQL
SELECT DATEDIFF([1], [2]) AS days_diff, TIMESTAMPDIFF([3], [2], [1]) AS months_diff, TIMESTAMPDIFF(YEAR, [2], [1]) AS years_diff;
Drag options to blanks, or click blank then click option'
A'2024-12-31'
B'2023-01-01'
CMONTH
DDAY
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping dates causing negative differences.
Using wrong units like 'DAY' for months difference.

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