Premium problem73. Average Interpurchase Time

Medium Locked

orders(order_id, user_id, ordered_at)

For each user with at least two orders, return the average number of hours between consecutive orders, rounded to two decimals. Ties on ordered_at are broken by order_id.

Return user_id and avg_hours_between_orders, ordered by user_id.

With n orders there are n - 1 gaps, so the answer is the whole span divided by n - 1. Dividing by n is the easy way to be slightly wrong everywhere.

Tables

orders

order_id  user_id  ordered_at
--------  -------  -------------------
1         10       2026-01-01 00:00:00
2         10       2026-01-02 00:00:00
3         10       2026-01-04 00:00:00
4         11       2026-01-01 00:00:00
5         11       2026-01-01 01:30:00
6         12       2026-01-01 00:00:00

Expected result

user_id  avg_hours_between_orders
-------  ------------------------
10       36.00
11       1.50

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query