DATE_SUB()

SQL · Date Functions

Theory

DATE_SUB moves a date backward by an interval. DATE_SUB('2026-11-08', INTERVAL 7 DAY) gives 2026-11-01. It is the mirror image of DATE_ADD and accepts the same units: days, weeks, months, years and, on timestamps, hours or minutes.

DATE_SUB is MySQL (and BigQuery) spelling. In SQL Server and Snowflake you write DATEADD(day, -7, date) with a negative number. PostgreSQL uses date - INTERVAL '7 days', Oracle date - 7, and SQLite date(d, '-7 days'). Two classic uses: computing a reminder date (send a notice 3 days before due_date) and building the start of a lookback window, like WHERE order_date >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) for 'last 30 days'. Be clear whether the window should include today and the boundary day, because off-by-one errors are the most common mistake here.

Why we use it

Many questions look backwards: last 30 days of sales, 3-day reminders before a deadline, data older than a year to archive. DATE_SUB computes those earlier dates correctly on the calendar.

When to use it

Use it for lookback windows, reminder dates and retention cutoffs. If your database lacks DATE_SUB, add a negative interval. For the number of days between two dates use DATEDIFF, not subtraction of intervals.

How it works

  1. Start with a date, e.g. due_date
  2. Choose how far back: 3 DAY, 1 MONTH...
  3. Database subtracts on the calendar
  4. Used as a window start? Yes: Keep rows with date >= start. No: Show it as a column.
  5. Return the earlier date or filtered rows

Syntax

DATE_SUB(date, INTERVAL n unit)

Example

SELECT DATE_SUB(hire_date, INTERVAL 30 DAY) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic