Customer Lifetime Spend

4pts
Walmart

Problem Statement

Walmart marketing teams want to identify high-value customers by looking at their total lifetime spend.

Write an SQL query to calculate the total amount spent by each customer since they signed up for the Walmart+ program.

Rules:

  • Include all customers from the Customers table, even those who have never placed an order.
  • For customers with no orders, the total spend should be 0.
  • Output columns: customer_name, total_lifetime_spend.
  • Results must be sorted by total_lifetime_spend in descending order.
Tests your understanding of
Aggregation, Joining and COALESCE

Input Tables

Customers
customer_id(INTEGER)customer_name(VARCHAR)signup_date(DATE)
1John Doe2024-01-01
2Jane Smith2024-01-05
3Bob Wilson2024-02-10
4Alice Brown2024-02-15
5Charlie Davis2024-03-01
6Eve Miller2024-03-05
Orders
order_id(INTEGER)customer_id(INTEGER)order_amount(DECIMAL)order_date(DATE)
1011150.52024-01-10
1021502024-02-01
1032300.252024-01-20
10431202024-03-01
1051752024-03-10
10645002024-03-15
Returns
return_id(INTEGER)order_id(INTEGER)return_reason(VARCHAR)
1102Damaged
2105Wrong item

Expected Output

customer_name(VARCHAR)total_lifetime_spend(DECIMAL)
Alice Brown500
Jane Smith300.25
John Doe275.5
Bob Wilson120
Charlie Davis0
Eve Miller0

Tags

MediumAggregationJoiningCOALESCE
10-15 min
70%

Hints