Stop 04 · Functions & ExpressionsTopics · 5/8

Date Arithmetic and Intervals

Part of the SQL Roadmap

In short

How many days did delivery take? When is a 30-day return window over? Which customers haven't ordered in 90 days? All of these are maths on dates.

You can add or subtract time from dates and find the gap between two dates. PostgreSQL uses intervals (order_date + INTERVAL '7 days'), while MySQL uses DATE_ADD and DATEDIFF.

The jaadu stamps the inside cover of a library book with a big rubber date stamp at a wooden issue counter.

Think of it like this

The library due-date stamp

The college librarian stamps your book: issued 3 March, due 14 days later on 17 March. Return it on 20 March and you're 3 days late. Adding days to a date and subtracting two dates is exactly what date arithmetic does.

Watch it happen

Same date maths, two dialects

PostgreSQL

  • Add: order_date + INTERVAL '7 days'
  • Add days to a DATE: order_date + 7
  • Gap in days: delivered_on - order_date
  • Readable gap: AGE(end, start)

MySQL

  • Add: order_date + INTERVAL 7 DAY
  • Or DATE_ADD(order_date, INTERVAL 7 DAY)
  • Gap in days: DATEDIFF(delivered_on, order_date)
  • Other units: TIMESTAMPDIFF(MONTH, start, end)

The idea is identical: a date plus a length of time is a new date, and a date minus a date is a gap. Only the spelling changes.

The big ideas

What you need to know

Date plus interval

order_date + INTERVAL '30 days' gives the return deadline. Intervals can be days, months, hours or a mix.

Date minus date

In PostgreSQL, two DATEs subtract to a whole number of days. Two TIMESTAMPs give an interval like 2 days 04:00.

Look back from today

WHERE order_date >= CURRENT_DATE - INTERVAL '30 days' finds the last 30 days of orders.

Months are uneven

31 January + 1 month is 28 February in non-leap years. PostgreSQL clamps to the last valid day.

See it in code

Walk through the code

sql
1SELECT2  order_id,3  order_date,4  delivered_on,5  delivered_on - order_date                    AS days_to_deliver,6  order_date + INTERVAL '30 days'              AS return_deadline,7  (order_date + 30) < CURRENT_DATE             AS return_window_closed8FROM orders9WHERE order_date >= CURRENT_DATE - INTERVAL '90 days';10 11-- Customers who haven't ordered in 90 days12SELECT customer_id, MAX(order_date) AS last_order13FROM orders14GROUP BY customer_id15HAVING MAX(order_date) < CURRENT_DATE - 90;

1/4DATE minus DATE gives a plain number of days in PostgreSQL.

PostgreSQL, with DATE columns. In MySQL use DATEDIFF(delivered_on, order_date), order_date + INTERVAL 30 DAY and CURRENT_DATE - INTERVAL 90 DAY.

Save yourself the pain

Common mistakes

Oops

Subtracting timestamps and expecting a number of days.

Do this instead

In PostgreSQL that gives an interval. Use EXTRACT(EPOCH FROM end_ts - start_ts) / 86400 for days, or cast both to date first.

Oops

Writing INTERVAL 7 DAY in PostgreSQL or INTERVAL '7 days' in MySQL.

Do this instead

PostgreSQL quotes the whole interval: INTERVAL '7 days'. MySQL writes INTERVAL 7 DAY. Check which database you're on.

Your mission

Deadlines and gaps

~25 min

Quick check

In PostgreSQL, what is DATE '2026-03-20' - DATE '2026-03-17'?

Go deeper

  1. 1Find orders placed in the last 30 days relative to CURRENT_DATE.
  2. 2Calculate how many days each order took to ship.
  3. 3Compute each customer's age in days since signup.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge