Learn SQL Lesson

Date Arithmetic with DATE_DIFF

SQL can calculate the distance between two dates. In DuckDB, `DATE_DIFF` is a convenient way to do that.

SELECT order_id, DATE_DIFF('day', order_date, ship_date) AS days_to_ship
FROM orders

This tells you how many days passed between each order and shipment.

Date arithmetic is useful for lead time, retention, overdue tasks, subscription length, and many other business questions.

Practice challenge

Return order_id and the number of shipping days as days_to_ship, ordered by order_id.

Open interactive editor