Chapter 9 · Interview Level
40. Compare Each Order to Previous and Next
Hard40 XPE-Commerce
For each customer's orders (ordered by date), show the current order's total_amount alongside the previous order's amount and the next order's amount. Also compute the change from the previous order. Return customer_id, order_date, total_amount, prev_amount, next_amount, and amount_change. Orders on the same date are ordered by order id. Sort by customer_id, then order_date.
Tables
- PKidINT
- FKcustomer_id→ customers.idINT
- order_dateTEXT
- statusTEXT
- total_amountREAL
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.