← All SQL interview questions

Win-back orders after a long gap

MediumWindow FunctionsSwiggyZomato

The growth team wants to find win-back orders: orders placed more than 7 days after the same customer's previous order.

From food_orders, return customer_id, order_id, order_date, prev_order_date and days_since_prev for every such order. A customer's first order has no previous order and must not appear. Order by customer_id, then order_date.

Sample data

Table: food_orders

14 row(s) · showing first 8

order_idINTcustomer_idINTorder_dateDATE
112026-09-01
212026-09-04
312026-09-20
412026-09-22
522026-09-02
622026-09-18
722026-10-05
1422026-10-06