What if you could instantly find any date before or after another without mistakes or extra work?
Why DATE_ADD and DATE_SUB in MySQL? - Purpose & Use Cases
Imagine you have a list of events with dates, and you need to find the date exactly 10 days after each event to send reminders.
Doing this by hand means calculating each date separately, which is slow and tiring.
Manually adding or subtracting days from dates is error-prone and time-consuming.
It's easy to make mistakes, especially with months having different lengths or leap years.
Also, doing this for many records by hand is impossible.
Using DATE_ADD and DATE_SUB functions lets you add or subtract days, months, or years directly in your database queries.
This automates date calculations accurately and quickly for many records at once.
Calculate new_date = old_date + 10 days for each record manually
SELECT DATE_ADD(event_date, INTERVAL 10 DAY) AS reminder_date FROM events;You can easily calculate future or past dates for any number of records, enabling automated scheduling and reminders.
A company wants to send a follow-up email 7 days after a customer's purchase date. Using DATE_ADD, they can find that date for all customers in one query.
Manual date calculations are slow and error-prone.
DATE_ADD and DATE_SUB automate adding or subtracting time intervals.
This makes scheduling and date-based queries easy and reliable.
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
