Learn SQL/Intermediate/Date & Time/SQL Subtract Days from a Date

SQL Subtract Days from a Date

The mirror of adding days — a plain minus sign, useful for computing 'look-back' dates.

What & Why

date_column - n subtracts n days. This is exactly the mechanism behind "filter to the last 30 days" style queries, covered explicitly a few lessons ahead.

See How It Works

BUSINESS QUESTION

Marketing wants the seven-day preparation date before each campaign begins.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
SELECT
  id AS campaign_id,
  start_date,
  start_date - INTERVAL '7 days' AS preparation_date
FROM marketing.campaigns
ORDER BY campaign_id;
RESULT — exact output from the displayed Queryflo rows
campaign_idstart_datepreparation_date
12024-01-152024-01-08 00:00:00
22024-02-102024-02-03 00:00:00
32024-03-122024-03-05 00:00:00
42024-04-012024-03-25 00:00:00

Each preparation timestamp is seven days before campaign start.

Now You Try

Practice this concept

Marketing wants the seven-day preparation date before each campaign begins.

Available schema
marketing

Prefix tables with marketing.table_name.

campaign_idstart_datepreparation_date
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario