Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Analyze Customer Purchase Trends with Window Functions

Medium
SQL & Data Manipulation
Asked 4d ago|
Jpmorgan Chase
Jpmorgan Chase
Asked 126 times

Problem

An online retail company wants to analyze customer spending trends over months. Write a SQL query to calculate the total spending for each customer by month, along with the cumulative spending for each customer over time.

Requirements

  1. Join the customers and orders tables on customer_id.
  2. Group the results by customer and month of the order date.
  3. Calculate total spending per customer per month.
  4. Use a window function to calculate cumulative spending for each customer.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique identifier for each customer
customer_nameVARCHAR(255)Name of the customer
orders
ColumnTypeDescription
order_idPKINTUnique identifier for each order
customer_idINTIdentifier linking to the customer who made the order
order_dateDATEDate when the order was placed
amountDECIMAL(10,2)Total amount of the order

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
SELECT ...
FROM ...
JOIN ... ON ...
GROUP BY ...
HAVING ...
ORDER BY ... DESC;
Sign up to unlock solutions
DigitalOcean Data Analyst Interview QuestionsAir Culinaire Worldwide Interview QuestionsThe Home Depot Interview QuestionsBarclays Data Scientist Interview QuestionsIllumina Data Analyst Interview Questions
Next questions
Total Wine & MoreMonthly Sales Trends by CategoryMediumZulilyAnalyze Monthly Revenue Trends by Product CategoryMediumPoint72SQL Window Functions for Top CategoriesHard
PostgreSQL