Menu
CoddyTech

Days and Intervals

Part of the SQL Foundations section of Coddy's PostgreSQL journey. Lesson 7 of 22.

Subtracting one date from another yields a number of days. Adding an integer to a date moves it by that many days. An interval represents a calendar or clock duration; adding it to a date produces a timestamp.

SELECT DATE '2025-03-10' - DATE '2025-03-07' AS elapsed, (DATE '2025-03-10' + INTERVAL '2 days')::date AS due;

elapsed is 3 and due is 2025-03-12. The final date cast removes the time component created by interval addition. Calendar arithmetic handles month boundaries for you.

Use date subtraction for elapsed days and interval addition for calendar offsets.

challenge icon

Challenge

Medium

Return id and due_date, exactly seven days after each order date. Return a date rather than a timestamp, ordered by id.

The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.

Try it yourself

-- Write your SQL here.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in SQL Foundations

Practice on your own: PostgreSQL playground