E-commerce: Total Amount Spent Per Customer

SQL coding challenge · Difficulty: medium · +100 XP

Problem

Finance needs a spending leaderboard. Show the total amount each customer has spent. Customers who never ordered → show 0.

Tables

Table: Customer

| customer_id | name | email |
| --- | --- | --- |
| 1 | Prince | prince@gmail.com |
| 2 | Mohit | mohit@gmail.com |
| 3 | Rahul | rahul@gmail.com |
| 4 | Krishna | krishna@gmail.com |

Table: Orders

| order_id | customer_id | order_date | total_amount |
| --- | --- | --- | --- |
| 101 | 1 | 2024-05-01 | 1500 |
| 102 | 2 | 2024-05-02 | 2300 |
| 103 | 3 | 2024-05-03 | 1800 |
| 104 | 4 | 2024-05-04 | 2700 |
| 105 | 1 | 2024-05-05 | 1100 |

Expected Output

| name | total_spent |
| --- | --- |
| Krishna | 2700 |
| Mohit | 2300 |
| Prince | 2600 |
| Rahul | 1800 |
  • Return: customer_id, name, total_spent
  • Customers with no orders → 0.00
  • Sort by total_spent descending

Solve this challenge on PySpark.in