Lesson

MySQL DATE_SUB() Function

10 min reading
Free Course

MySQL DATE_SUB(): Subtract Time Intervals From Dates

The DATE_SUB() function subtracts a specified time or calendar interval from a DATE or DATETIME expression. It serves as the direct counterpart to DATE_ADD() and shares the same interval units and month-end clamping rules.

Analytics teams use DATE_SUB() to build rolling 7-day, 30-day, and year-over-year lookback windows.

Syntax

DATE_SUB() requires two arguments with the INTERVAL keyword:

DATE_SUB(date_value, INTERVAL expr unit)
  • date_value: A DATE, DATETIME, TIMESTAMP, or valid date string.
  • INTERVAL expr unit: Supports units such as DAY, WEEK, MONTH, QUARTER, YEAR, HOUR, MINUTE, and SECOND.

Practical Example

Suppose you want to inspect recent activity in demo_orders by computing a 7-day warning date and a 1-month prior comparison date for each order.

SELECT
    order_id,
    order_date,
    DATE_SUB(order_date, INTERVAL 7 DAY) AS reminder_sent_date,
    DATE_SUB(order_date, INTERVAL 1 MONTH) AS prior_month_date
FROM demo_orders
WHERE order_id IN (210, 211, 212);
order_id order_date reminder_sent_date prior_month_date
210 2026-03-31 2026-03-24 2026-02-28
211 2026-05-05 2026-04-28 2026-04-05
212 2026-05-12 2026-05-05 2026-04-12

Row 210 demonstrates backward month clamping: subtracting 1 MONTH from 2026-03-31 lands safely on 2026-02-28.

Common Mistakes & NULL Behavior

Keep these rules in mind when calling DATE_SUB():

  • Requires INTERVAL keyword: Unlike SUBDATE(date, 7), writing DATE_SUB(order_date, 7) causes a syntax error.
  • Returns NULL for NULL or invalid dates: Passing NULL, '2026-02-30', or '2026-13-01' causes DATE_SUB() to return NULL.
  • Double negatives add time: Passing a negative interval like DATE_SUB('2026-05-12', INTERVAL -5 DAY) moves the date forward to '2026-05-17'.
  • Keep column indexed in WHERE: Write WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) instead of applying functions to order_date. That way, MySQL evaluates DATE_SUB() once and uses any index on order_date.

Frequently Asked Questions

Is DATE_SUB(d, INTERVAL 1 DAY) the same as d - INTERVAL 1 DAY?

Yes. Both expressions evaluate identically and return the exact same DATE or DATETIME type.

What is the difference between DATE_SUB() and SUBDATE()?

DATE_SUB() always needs INTERVAL expr unit. SUBDATE() accepts INTERVAL expr unit as well as the shorter integer syntax SUBDATE(date, days).

What happens if I subtract 2 hours from a DATE column?

MySQL promotes the DATE to midnight 'YYYY-MM-DD 00:00:00' and returns a DATETIME on the previous calendar day at 22:00:00.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum