Next Order Amount (LEAD)

Medium ⏱ 12 min 46% acceptance ★★★★☆ 4.3
For each order, return customer_id, order_id, amount and next_amount — that same customer's next order amount (NULL for their last), by customer then date. LEAD is LAG's mirror image, and interviewers check you know which is which.

Database Schema

13 tables — click to view columns

Use these exact table and column names in your query. Open Schema & Data under the editor to preview sample rows.

customers 5 cols
customer_idINTEGERnameTEXTcityTEXTcountryTEXTsignup_dateDATE
products 4 cols
product_idINTEGERproduct_nameTEXTcategoryTEXTpriceREAL
orders 5 cols
order_idINTEGERcustomer_idINTEGERorder_dateDATEamountREALstatusTEXT
order_items 4 cols
order_idINTEGERproduct_idINTEGERquantityINTEGERunit_priceREAL
payments 5 cols
payment_idINTEGERorder_idINTEGERmethodTEXTamountREALpaid_dateDATE
employees 6 cols
employee_idINTEGERnameTEXTdepartmentTEXTmanager_idINTEGERsalaryREALhire_dateDATE
logins 2 cols
user_idINTEGERlogin_dateDATE
ratings 5 cols
rating_idINTEGERcustomer_idINTEGERproduct_idINTEGERratingINTEGERrating_dateDATE
subscriptions 6 cols
sub_idINTEGERcustomer_idINTEGERplanTEXTstart_dateDATEend_dateDATEmonthly_feeREAL
shipments 5 cols
shipment_idINTEGERorder_idINTEGERcarrierTEXTship_dateDATEdelivery_dateDATE
support_tickets 7 cols
ticket_idINTEGERcustomer_idINTEGERsubjectTEXTpriorityTEXTstatusTEXTopened_dateDATEclosed_dateDATE
coupons 5 cols
codeTEXTdiscount_pctREALmin_order_amountREALvalid_fromDATEvalid_toDATE
coupon_redemptions 4 cols
redemption_idINTEGERcodeTEXTorder_idINTEGERredeemed_dateDATE

Constraints

  • LEAD partitioned per customer, ordered by date then order_id.
  • Order by customer_id, order_date, order_id.

Topics

Window Functions

Companies

AmazonUber

Hints

Hint 1

LEAD(amount) OVER (PARTITION BY customer_id ORDER BY order_date, order_id).

Press Run (or Ctrl+Enter) to execute your query against the sample database.
customers5 cols
  • customer_idINTEGER
  • nameTEXT
  • cityTEXT
  • countryTEXT
  • signup_dateDATE
customer_idnamecitycountrysignup_date
101RahulDelhiIndia2023-01-12
102AmanMumbaiIndia2023-02-03
103SaraBengaluruIndia2023-02-20
products4 cols
  • product_idINTEGER
  • product_nameTEXT
  • categoryTEXT
  • priceREAL
product_idproduct_namecategoryprice
1Wireless MouseElectronics799
2Office ChairFurniture5499
3NotebookStationery149
orders5 cols
  • order_idINTEGER
  • customer_idINTEGER
  • order_dateDATE
  • amountREAL
  • statusTEXT
order_idcustomer_idorder_dateamountstatus
10011012024-01-051598Delivered
10021022024-01-115499Delivered
10031012024-02-02149Cancelled
order_items4 cols
  • order_idINTEGER
  • product_idINTEGER
  • quantityINTEGER
  • unit_priceREAL
order_idproduct_idquantityunit_price
100112799
1002215499
100435149
payments5 cols
  • payment_idINTEGER
  • order_idINTEGER
  • methodTEXT
  • amountREAL
  • paid_dateDATE
payment_idorder_idmethodamountpaid_date
50011001UPI15982024-01-05
50021002Card54992024-01-11
50031005UPI9992024-03-02
employees6 cols
  • employee_idINTEGER
  • nameTEXT
  • departmentTEXT
  • manager_idINTEGER
  • salaryREAL
  • hire_dateDATE
employee_idnamedepartmentmanager_idsalaryhire_date
1MeeraAnalyticsNULL180000
2RohitAnalytics195000
3NehaEngineering1120000
logins2 cols
  • user_idINTEGER
  • login_dateDATE
user_idlogin_date
1012024-06-01
1012024-06-02
1022024-06-01
ratings5 cols
  • rating_idINTEGER
  • customer_idINTEGER
  • product_idINTEGER
  • ratingINTEGER
  • rating_dateDATE
rating_idcustomer_idproduct_idratingrating_date
1101152024-01-10
2101442024-03-12
3102252024-01-15
subscriptions6 cols
  • sub_idINTEGER
  • customer_idINTEGER
  • planTEXT
  • start_dateDATE
  • end_dateDATE
  • monthly_feeREAL
sub_idcustomer_idplanstart_dateend_datemonthly_fee
1101Basic2023-02-012023-07-31499
2101Pro2023-08-01NULL999
3102Pro2023-03-152024-03-14999
shipments5 cols
  • shipment_idINTEGER
  • order_idINTEGER
  • carrierTEXT
  • ship_dateDATE
  • delivery_dateDATE
shipment_idorder_idcarriership_datedelivery_date
11001Delhivery2024-01-062024-01-09
21002BlueDart2024-01-122024-01-14
151018Ekart2024-09-16NULL
support_tickets7 cols
  • ticket_idINTEGER
  • customer_idINTEGER
  • subjectTEXT
  • priorityTEXT
  • statusTEXT
  • opened_dateDATE
  • closed_dateDATE
ticket_idcustomer_idsubjectprioritystatusopened_dateclosed_date
1101Late deliveryHighClosed2024-01-102024-01-11
2102Wrong item receivedHighClosed2024-01-202024-01-23
10110Wrong address deliveredUrgentOpen2024-05-01NULL
coupons5 cols
  • codeTEXT
  • discount_pctREAL
  • min_order_amountREAL
  • valid_fromDATE
  • valid_toDATE
codediscount_pctmin_order_amountvalid_fromvalid_to
WELCOME10105002024-01-012024-12-31
SAVE202020002024-03-012024-06-30
FLASH505010002024-04-012024-04-15
coupon_redemptions4 cols
  • redemption_idINTEGER
  • codeTEXT
  • order_idINTEGER
  • redeemed_dateDATE
redemption_idcodeorder_idredeemed_date
1WELCOME1010012024-01-05
2SAVE2010062024-03-09
3WELCOME1010132024-06-25
Loading expected output…