CodeBlanks Logo Icon CodeBlanks

Date arithmetic

Add and subtract days, months from a date.

Date arithmetic

To add days to a date you use date + INTERVAL '7' DAY. PostgreSQL supports this form.

SELECT order_date + INTERVAL '7' DAY AS hafta_sonra FROM orders;

This query adds 7 days to the order date.

order_date + INTERVAL '7' DAY AS hafta_sonra is selected from orders.

Date arithmetic — details

MySQL uses DATE_ADD(date, INTERVAL 7 DAY). To subtract, give a negative interval.

Practice Date arithmetic

Add 7 days to order_date aliased as week_later from orders.

Starter code

query.sql
-- add 7 days
SELECT
FROM orders

Sample solution

query.sql
SELECT order_date + INTERVAL '7' DAY AS hafta_sonra
FROM orders

Expected output

hafta_sonra
2026-01-22
2026-02-05

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Date arithmetic: match the terms

Term Meaning
INTERVAL interval
DAY day
MONTH month
YEAR year

2 Interval

Question
SELECT order_date + ____ '7' DAY FROM orders;
Answer
SELECT order_date + INTERVAL '7' DAY FROM orders;

Explanation: INTERVAL is the right answer.

3 What adds 7 days?

  • + 7
  • INTERVAL '7' DAY Correct answer
  • ADD 7
  • PLUS 7

Explanation: INTERVAL '7' DAY adds days.

All lessons in Beginner