001Level 1

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.

Peek at the solution?

You can still earn the XP afterwards by submitting a working query — but you'll learn more if you give it one more try.

Chapter 9 · Interview Level

0/62
  1. 1Customers with Missing Phone Numbers
  2. 2Customer Contact with COALESCE Fallback
  3. 3Classify Orders by Size
  4. 4Extract Year from Order Dates
  5. 5Uppercase Product Names
  6. 6Combine Cancelled and Returned Orders
  7. 7Customers Who Have Written Reviews
  8. 8Customer Count by State
  9. 9Second Most Expensive Product
  10. 10Patient Contact Info with COALESCE
  11. 11Patient Count by Email Domain
  12. 12Current Patient Age Calculation
  13. 13Each Customer's Most Recent Order Amount
  14. 14Average Days to Ship by Status
  15. 15Product Pairs in the Same Category
  16. 16Orders That Were Never Shipped
  17. 17Revenue Pivot by Payment Method Per Month
  18. 18Cumulative Spending Per Customer
  19. 19Top-Selling Category by Quantity
  20. 20Each Product's Share of Total Revenue
  21. 21Customers Using Multiple Payment Methods
  22. 223-Order Moving Average Per Customer
  23. 23Combined Cash Inflow Report
  24. 24Average Days Between Loan Payments
  25. 25Prescription Duration in Days
  26. 26Visit Status Summary with CASE
  27. 27Combined Doctor and Patient Contact List
  28. 28Patients with Visits but No Lab Results
  29. 29Doctors Who Treated Every Blood Type
  30. 30Revenue Pivot by Payment Method
  31. 31Average Delivery Time by Carrier
  32. 32Top 3 Best-Selling Products Per Category
  33. 33Customer Cohort Analysis by First Purchase Month
  34. 34RFM Customer Segmentation
  35. 35Market Basket: Frequently Co-Purchased Products
  36. 36Monthly Cohort Retention Analysis
  37. 37Inventory Reorder Alert with Sales Velocity
  38. 38Customer Churn Risk Scoring
  39. 39Deduplicate Reviews with ROW_NUMBER
  40. 40Compare Each Order to Previous and Next
  41. 41Assign Customers to Spending Quartiles
  42. 42Order Date Gap Analysis
  43. 43Monthly Cohort Retention Rate
  44. 44Customer Consecutive Order Month Streaks
  45. 45Refund Rate and Revenue Impact Analysis
  46. 46Monthly Deposits vs Withdrawals
  47. 47Loan Payment Coverage Ratio
  48. 48Detect Suspicious Transaction Patterns
  49. 49Transaction Type Pivot by Account
  50. 50Loan Risk Classification with CASE
  51. 51Customers with Both Accounts and Loans (EXISTS)
  52. 52Executive Financial Dashboard Query
  53. 53Detect Suspicious Large Withdrawal Patterns
  54. 54Account Cohort Activity Analysis
  55. 55Find Duplicate Patient Emails
  56. 56Department Budget Utilization
  57. 57Comprehensive Doctor Scorecard
  58. 58Follow-Up Visit Chains
  59. 59Pivot Visit Counts by Status per Doctor
  60. 60Patient Risk Categorization
  61. 61Comprehensive Hospital Performance Report
  62. 62Department Performance Dashboard
See the full learning path →