Date Operations & Time-Based Analytics SQL Topic exerciseEasyVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Calculate Days Between Order and Shipping

Return each shipped order with the whole number of days from order_date to ship_date.

  • Date analysis
  • Type conversion
  • NULL handling
  • Filtering
  • Sorting

Exercise brief

Understand the request

Fulfillment operations analyst The weekly carrier scorecard needs elapsed calendar days for every shipped order.

The weekly carrier scorecard needs elapsed calendar days for every shipped order. Return each shipped order with the whole number of days from order_date to ship_date.

Return

  • Return order_id, customer_id, order_date, ship_date, days_to_ship in this exact left-to-right order.

Constraints

  • Exclude rows whose ship_date is NULL.
  • Use date arithmetic rather than subtracting formatted text.
  • Order by order_id ascending.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

orders

  • order_idINTEGER
  • customer_idINTEGER
  • order_dateDATE
  • ship_dateDATE
  • order_totalDECIMAL
  • order_timestampDATETIME

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

Date difference = number of days between two dates. Each engine spells this differently.

Hint 2

SQLite: julianday(a) - julianday(b) returns a float number of days — CAST to INTEGER.

Hint 3

Always guard against NULL ship_date so unshipped orders are excluded (or show NULL).

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT order_id, customer_id, order_date, ship_date, CAST(julianday(ship_date) - julianday(order_date) AS INTEGER) AS days_to_ship FROM orders WHERE ship_date IS NOT NULL ORDER BY order_id;

Why this works

Day-difference is the most common date task and the most engine-divergent. SQLite has no DATEDIFF, so you subtract julianday() values. Postgres lets you subtract dates directly (yields an integer). MySQL has DATEDIFF(end, start). SQL Server has DATEDIFF(DAY, start, end) — note the unit comes first and the argument order is reversed from MySQL.

Success check

Every shipped order appears once with the correct non-negative days_to_ship value in stable order.

Expected result

Use this output to verify values, aliases, ordering, and row count.

order_idcustomer_idorder_dateship_datedays_to_ship
112024-01-152024-01-183
222024-01-202024-01-233
312024-02-102024-02-155
432024-02-252024-02-283
522024-03-052024-03-083
642024-03-152024-03-205
712024-04-012024-04-054
852024-04-102024-04-122
932024-05-052024-05-083
1022024-05-202024-05-233

Previewing 10 of 20 expected rows. Run the query in the editor to inspect the full result.

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.