PostgreSQL lets you subtract two dates or use the age() function to return the exact interval between them.
Subtract one DATE from another. PostgreSQL returns an integer showing the day difference, positive or negative.
SELECT order_date - created_at AS days_to_purchase
FROM Orders o
JOIN Customers c ON c.id = o.customer_id;
Use age() when you need a full interval (years, months, days).age() normalizes months and years, making it ideal for reporting customer tenure.
SELECT age(current_date, created_at) AS customer_age
FROM Customers;
Cast the dates to TIMESTAMP and subtract; PostgreSQL returns an INTERVAL you can extract parts from.
SELECT EXTRACT(EPOCH FROM (order_date - created_at))/3600 AS hours_gap
FROM Orders;
Yes.Subtract and average the INTERVAL, then display in days with EXTRACT.
SELECT ROUND(AVG(EXTRACT(EPOCH FROM (shipped_at - order_date))/86400),2) AS avg_cycle_days
FROM Orders;
Store dates in UTC, use DATE for day-level math, TIMESTAMP WITH TIME ZONE for sub-day precision, and index date columns for speed.
Confusing age() output or mixing TIMESTAMP types causes wrong results. Always inspect the returned data type and cast explicitly.
.
Yes. Subtracting two DATE values is a simple integer operation, usually faster than calling age(), which builds an INTERVAL.
Both subtraction and age() handle leap years automatically. February 29 is treated as an extra day when present.
Use a generated column storing the difference and index that column, or create a functional index on (order_date - created_at).