DATE_SUB() in MySQL — How It Works

What the MySQL DATE_SUB() function does, with an example.

Published October 19, 2026

DATE_SUB(date, INTERVAL n unit) subtracts an interval from a date or datetime.

How it works

Great for 'last 30 days' filters.

Try it in MySQL

SELECT DATE_SUB('2026-11-12', INTERVAL 30 DAY)
2026-10-13
SELECT * FROM orders WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
last week
SELECT NOW() - INTERVAL 1 YEAR
a year ago

Practical examples

Example 1

SELECT DATE_SUB('2026-11-12', INTERVAL 30 DAY);
-- 2026-10-13

Example 2

SELECT * FROM orders WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY);
-- last week

Example 3

SELECT NOW() - INTERVAL 1 YEAR;
-- a year ago

Use it in a real query

SELECT id, DATE_SUB('2026-11-12', INTERVAL 30 DAY) AS result
FROM your_table
LIMIT 10;

Common mistakes

  • Wrapping the indexed column instead of the constant.
  • Wrong unit names.
  • Using it on non-date strings.

Key takeaways

  • DATE_SUB() in one line: DATE_SUB(date, INTERVAL n unit) subtracts an interval from a date or datetime.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

Last month?

DATE_SUB(CURDATE(), INTERVAL 1 MONTH).

Does DATE_SUB() work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles