Learn SQL/Intermediate/Date & Time/SQL Date Arithmetic

SQL Date Arithmetic

Add or subtract a number of days directly from a date using plain + and - operators — dates behave like numbers in this specific way.

What & Why

Postgres lets you add or subtract a plain integer directly to or from a DATE, and it's interpreted as a number of days. start_date + 7 means "seven days after start_date." This is the simplest form of date math — the INTERVAL lesson right after this one covers a more flexible, explicit way to express the same kind of arithmetic.

See How It Works

BUSINESS QUESTION

Marketing wants seven-day and thirty-day checkpoints derived from every campaign start date.

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
Watch one date move by integer day offsetsStep 1 of 3
start_date + 7
idnamestart_dateplus_7_days
1Spring Launch2024-01-152024-01-22
2Retention Webinar2024-02-102024-02-17
3Finance Retargeting2024-03-122024-03-19
4Enterprise Search2024-04-012024-04-08

Adding the integer 7 moves every displayed campaign DATE forward by exactly seven days.

EXAMPLE QUERY
SELECT
  id,
  name,
  start_date,
  start_date + 7 AS plus_7_days,
  start_date - 7 AS minus_7_days,
  start_date + 30 AS plus_30_days
FROM marketing.campaigns
ORDER BY id;
Now You Try

Practice this concept

Marketing wants seven-day and thirty-day checkpoints derived from every campaign start date.

Available schema
marketing

Prefix tables with marketing.table_name.

idnamestart_dateplus_7_daysminus_7_daysplus_30_days
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