Return Each Amazon Fresh Customer's Second Order and Days Since First
Last updated: November 29, 2026
Quick Overview
For every Amazon Fresh customer with at least two orders, return their second order's ID and date and the number of days between their first and second orders.
Amazon
November 29, 20260
0
0 solved
For every Amazon Fresh customer with at least two orders, return their second order's ID and date and the number of days between their first and second orders.
An online assessment style SQL item framed on grocery delivery, where time to second order is a key early repeat signal. The interviewer checks ROW_NUMBER with a deterministic order and LAG to get the previous order in the same pass.
What the Interviewer Expects
- Use ROW_NUMBER partitioned by customer and ordered by time
- Add a tiebreaker for orders placed at the same timestamp
- Use LAG to get the first order date on the second row
- Filter to row number 2 in an outer query
- Exclude cancelled orders
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you compute the median days to second order by signup month?
- How would you include customers with only one order and show null?
- What if two orders are placed on the same day as a split basket?
- How would you find the nth order generally?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Table fresh_orders(order_id, customer_id, order_ts, status). Assume cancelled orders do not count and timestamps can tie (for example when a basket is split by the system), so I break ties with `ord...
Approach
In one pass over each customer's orders sorted by time:
ROW_NUMBER()numbers the orders.LAG(order_ts)returns the previous order's time, which on row 2 is the first order.
Then keep rn = 2...