← All SQL interview questions
First and last purchase span per customer
HardWindow FunctionsAmazonRazorpay
A lifecycle analysis needs each customer's first and last purchase, the gap in days between them, and how their latest order compares with their own average.
From purchase_history, return one row per customer_code with first_purchase, last_purchase, days_active (days between the two dates), last_amount and avg_amount rounded to 2 decimals. Solve it with window functions rather than a GROUP BY roll-up.
Sample data
More Window Functions questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program