SQL INTERVAL

A dedicated type for expressing a SPAN of time — days, months, or years — that adds and subtracts correctly, unlike a plain integer.

What & Why

INTERVAL is its own data type representing a duration — INTERVAL '3 months', INTERVAL '1 year', INTERVAL '2 days'. Unlike plain integer arithmetic (which only ever means days), an interval can express units that don't have a fixed day count, and Postgres handles the calendar math correctly — adding a month to January 31st correctly lands on a real date, not an invalid one.

See How It Works

BUSINESS QUESTION

Growth wants to see day-, hour-, and calendar-month intervals applied to each real signup timestamp.

idcreated_atcountrychannelplanactivated_atchurned_at
1012024-01-15 09:10:00+00USorganicpro2024-01-16 14:25:00+00NULL
1022024-02-10 11:05:00+00CApaidfreeNULL2024-03-20 10:00:00+00
1032024-03-12 16:35:00+00GBreferralpro2024-03-13 08:15:00+00NULL
1042024-04-01 13:20:00+00USorganicfree2024-04-03 12:00:00+00NULL
Watch different interval units move one real timestampStep 1 of 3
created_at + INTERVAL '7 days'
idcreated_atactivation_deadline
1012024-01-15 09:10:00+002024-01-22 09:10:00+00
1022024-02-10 11:05:00+002024-02-17 11:05:00+00
1032024-03-12 16:35:00+002024-03-19 16:35:00+00
1042024-04-01 13:20:00+002024-04-08 13:20:00+00

The first query result column applies the seven-day interval to every displayed growth.users row.

EXAMPLE QUERY
SELECT
  id,
  created_at,
  created_at + INTERVAL '7 days' AS activation_deadline,
  created_at + INTERVAL '2 hours' AS two_hours_later,
  created_at + INTERVAL '1 month' AS one_month_later
FROM growth.users
ORDER BY created_at, id;
Now You Try

Practice this concept

Growth wants day, hour, and calendar-month intervals applied to every displayed signup timestamp.

Available schema
growth

Prefix tables with growth.table_name.

idcreated_atactivation_deadlinetwo_hours_laterone_month_later
growth.users
ColumnType
idinteger
created_attimestamp with time zone
countrytext
channeltext
plantext
activated_attimestamp with time zone
churned_attimestamp with time zone
legacy_user_codetext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario