DATE_ADD and DATE_SUB in MySQL - Time & Space Complexity
When using DATE_ADD and DATE_SUB in MySQL, it's helpful to know how the time to run these functions changes as the amount of data grows.
We want to see how the work done grows when we apply these date functions to many rows.
Analyze the time complexity of the following code snippet.
SELECT order_id, order_date,
DATE_ADD(order_date, INTERVAL 7 DAY) AS delivery_date
FROM orders;
This query adds 7 days to the order_date for each order to find the delivery date.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying DATE_ADD to each row's order_date.
- How many times: Once for every row in the orders table.
Each row requires one date addition operation, so the total work grows directly with the number of rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 date additions |
| 100 | 100 date additions |
| 1000 | 1000 date additions |
Pattern observation: The work increases in a straight line as the number of rows increases.
Time Complexity: O(n)
This means the time to run the query grows directly in proportion to the number of rows processed.
[X] Wrong: "DATE_ADD runs in constant time no matter how many rows there are."
[OK] Correct: The function runs once per row, so more rows mean more total work.
Understanding how simple functions like DATE_ADD scale with data size helps you explain query performance clearly and confidently.
"What if we added a JOIN that doubles the number of rows? How would the time complexity change?"
Practice
DATE_ADD(date, INTERVAL expr unit) do?Solution
Step 1: Understand the function purpose
The functionDATE_ADDis designed to add a time interval to a given date value.Step 2: Compare options with function behavior
'Adds a specified time interval to a date' correctly states that it adds a specified time interval. 'Subtracts a specified time interval from a date' describesDATE_SUB, notDATE_ADD. The options for returning the current date and time and formatting a date into a string describe unrelated functions.Final Answer:
Adds a specified time interval to a date -> Option CQuick Check:
DATE_ADD adds time interval [OK]
- Confusing DATE_ADD with DATE_SUB
- Thinking DATE_ADD formats dates
- Assuming DATE_ADD returns current date
Solution
Step 1: Check correct function and syntax for subtraction
To subtract days, useDATE_SUBwithINTERVALkeyword and unit DAY.Step 2: Validate each option's syntax
DATE_SUB('2024-06-15', INTERVAL 10 DAY) uses correct syntax:DATE_SUB('date', INTERVAL 10 DAY). DATE_ADD('2024-06-15', INTERVAL -10 DAY) usesDATE_ADDwith negative interval, which is not recommended. DATE_SUB('2024-06-15', 10 DAY) missesINTERVALkeyword. DATE_ADD('2024-06-15', 10 DAYS) misses theINTERVALkeyword and uses DATE_ADD which adds instead of subtracts.Final Answer:
DATE_SUB('2024-06-15', INTERVAL 10 DAY) -> Option BQuick Check:
Correct syntax uses DATE_SUB with INTERVAL [OK]
- Omitting INTERVAL keyword
- Using plural unit like DAYS instead of DAY
- Using DATE_ADD with negative interval
SELECT DATE_ADD('2024-06-10', INTERVAL 3 MONTH) AS new_date;Solution
Step 1: Understand DATE_ADD with MONTH interval
The query adds 3 months to the date '2024-06-10'. Adding 3 months to June 10, 2024, results in September 10, 2024.Step 2: Match the calculated date with options
2024-09-10 shows '2024-09-10', which matches the expected result. Other options are incorrect dates or no change.Final Answer:
2024-09-10 -> Option DQuick Check:
June 10 + 3 months = September 10 [OK]
- Adding days instead of months
- Confusing month addition with subtraction
- Expecting day to change unexpectedly
SELECT DATE_SUB('2024-06-20', 5 DAY);Solution
Step 1: Review DATE_SUB syntax
The correct syntax requires the INTERVAL keyword before the number and unit, likeDATE_SUB(date, INTERVAL expr unit).Step 2: Identify the missing keyword
The query usesDATE_SUB('2024-06-20', 5 DAY)missing INTERVAL, causing syntax error.Final Answer:
Missing INTERVAL keyword before 5 DAY -> Option AQuick Check:
DATE_SUB needs INTERVAL keyword [OK]
- Omitting INTERVAL keyword
- Using plural unit DAYS instead of DAY
- Confusing DATE_ADD and DATE_SUB
Solution
Step 1: Understand the requirement
We need the date 2 weeks before the last day of the current month.LAST_DAY(CURDATE())gives the last day of this month.Step 2: Apply correct date arithmetic
Subtracting 2 weeks from that date is done withDATE_SUB(..., INTERVAL 2 WEEK). SELECT DATE_SUB(LAST_DAY(CURDATE()), INTERVAL 2 WEEK) AS result; uses this correctly. SELECT DATE_ADD(LAST_DAY(CURDATE()), INTERVAL -2 WEEK) AS result; uses DATE_ADD instead of DATE_SUB. SELECT DATE_SUB(CURDATE(), INTERVAL 2 WEEK) AS result; subtracts from current date, not last day. SELECT DATE_ADD(LAST_DAY(CURDATE()), INTERVAL 2 WEEK) AS result; adds 2 weeks, which is incorrect.Final Answer:
SELECT DATE_SUB(LAST_DAY(CURDATE()), INTERVAL 2 WEEK) AS result; -> Option AQuick Check:
Subtract 2 weeks from last day using DATE_SUB [OK]
- Subtracting from current date instead of last day
- Using DATE_ADD with positive interval instead of subtraction
- Confusing weeks with days in interval
Ready to prove it?
Pass the test and this pattern moves unknown
