SQL · Level 3 Dates

SQL Date Arithmetic

Find the late deliveries by computing the days between two dates with julianday(), across months and leap years.

  • julianday(b) - julianday(a)
  • date(d, '+3 days')
  • Only works on ISO dates

SQLite does date maths on ISO dates: date('2024-03-30', '+3 days') is '2024-04-02', and julianday(b) - julianday(a) is the number of days between. Trap: this only works on YYYY-MM-DD dates; '03/04/2024' is just text. PostgreSQL writes b - a and d + INTERVAL '3 days'.

SELECT id, julianday(delivered) - julianday(ordered) AS days
FROM parcels;   -- 2024-03-30 → 2024-04-02 gives 3.0

Return the id and the number of days it took for every order delivered more than 3 days after it was ordered, ordered by id. In the database below, order 2 took 5 days across the end of March.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

orders 5 rows
idINTEGERordered_onTEXTdelivered_onTEXT
12024-03-012024-03-03
22024-03-282024-04-02
32024-02-272024-03-01
42024-03-102024-03-14
52024-03-20NULL
solution.sql
Test Suites Pending
Output