LQ
SQL Reference
Practice →
SQL ReferenceFunctionsDate Arithmetic

Date Arithmetic

Add, subtract, and compare dates.

Questions this answers
  • How do I calculate how many days between two dates?
  • How do I add 30 days to a date?
  • How do I find orders placed in the last 7 days?
  • How do I group by week or month?

How it works

Date arithmetic lets you calculate durations, shift dates forward or backward, and group events by time period. The syntax varies by database but the concepts are universal.

Difference between dates

-- Days between order and delivery
SELECT order_id,
       julianday(delivered_at) - julianday(ordered_at) AS days_to_deliver
FROM orders;

Add or subtract time

-- SQLite: orders in the last 30 days
SELECT * FROM orders
WHERE ordered_at >= date('now', '-30 days');

Group by month

SELECT strftime('%Y-%m', ordered_at) AS month,
       COUNT(*) AS orders
FROM orders
GROUP BY month
ORDER BY month;

Ready to practise Date Arithmetic?

Solve real exercises with AI feedback — free to start.

Try it free →