Unit 4, lesson 3 · Subtracting dates
Days between two dates
How long did delivery take?
Subtract one date from another and PostgreSQL gives the number of days between them as a whole number: DATE '2026-03-07' - DATE '2026-03-01' gives 6. Put the later date first, so the answer is positive. The example measures how long each customer had been signed up by the end of 2026.
SELECT name, signup_date,
DATE '2026-12-31' - signup_date AS days_as_customer
FROM customers
LIMIT 5;Your task
Operations wants to see delivery times for last month. For every delivered order placed in November 2026, return the id, order_date, delivered_date and the number of days from order to delivery as days_to_deliver.
Tables you will use: orders
Try it in your browserFree, no sign-up. Your query is checked as you go.