Learn SQL/Intermediate/Date & Time/SQL Days Between Dates

SQL Days Between Dates

The general form of the Subtract Dates lesson — computing the gap between any two arbitrary date columns, not just against today.

What & Why

Subtracting two DATE columns from each other gives the day count between them — this works identically whether one side is CURRENT_DATE or two entirely different stored columns.

See How It Works

BUSINESS QUESTION

Marketing wants each campaign duration in days.

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,
  name,
  end_date - start_date AS duration_days
FROM marketing.campaigns
WHERE end_date IS NOT NULL
ORDER BY duration_days DESC, campaign_id;
RESULT — exact output from the displayed Queryflo rows
campaign_idnameduration_days
4Enterprise Search90
3Finance Retargeting80
1Spring Launch76
2Retention Webinar65

DATE subtraction returns the real completed campaign durations.

Now You Try

Practice this concept

Marketing wants each campaign duration in days.

Available schema
marketing

Prefix tables with marketing.table_name.

campaign_idnameduration_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