Retail: Total Amount Spent Per Customer
SQL coding challenge · Difficulty: easy · Topic: Joins · +50 XP
Problem
A retail chain's loyalty program tracks customer spending. Show the total amount spent. Include customers who have never ordered (show 0).
Tables
Table: Customers
| customer_id | name | email | city | | --- | --- | --- | --- | | 1 | Rahul | rahul@gmail.com | Delhi | | 2 | Prince | prince@gmail.com | Mumbai | | 3 | Mohit | mohit@gmail.com | Bangalore | | 4 | Sudheer | sudheer@gmail.com | Hyderabad |
Table: Products
| product_id | product_name | category | price | | --- | --- | --- | --- | | 101 | T-Shirt | Apparel | 599.00 | | 102 | Jeans | Apparel | 1299.00 | | 103 | Sneakers | Footwear | 2499.00 | | 104 | Backpack | Accessories | 899.00 | | 105 | Watch | Accessories | 1499.00 |
Table: Orders
| order_id | customer_id | order_date | total_amount | payment_mode | | --- | --- | --- | --- | --- | | 201 | 1 | 2024-05-01 | 1898.00 | UPI | | 202 | 2 | 2024-05-02 | 2499.00 | Credit Card | | 203 | 3 | 2024-05-03 | 1399.00 | Debit Card | | 204 | 1 | 2024-05-04 | 599.00 | UPI | | 205 | 4 | 2024-05-05 | 3898.00 | Credit Card | | 206 | 2 | 2024-05-06 | 899.00 | UPI |
Expected Output
| name | total_spent | | --- | --- | | Sudheer | 3898.00 | | Prince | 3398.00 | | Rahul | 2497.00 | | Mohit | 1399.00 |
- Return:
name,total_spent - Include customers who never ordered (show 0)
- Sort by
total_spentdescending
What this SQL challenge teaches you
“Retail: Total Amount Spent Per Customer” is a easy-level SQL challenge focused on Joins. Working through it gives you hands-on practice with LEFT JOIN, SUM, GROUP BY, ORDER BY — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- LEFT JOIN
- SUM
- GROUP BY
- ORDER BY
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- Your result should return: name, total_spent.
- Order the output total_spent descending.
- Compare your output to the Expected Output — the columns, values and row order must match exactly.
Where this comes up
Variations of this problem have been reported in interviews at Flipkart, Myntra, Meesho. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Find Customers Missing from New Table
- Customers With at Least One Order
- Customers Who Have Never Placed an Order
- Count Orders Per Customer Including Zero
- Total Amount Spent Per Customer (With Zero)
- Find Employees in the HR Department
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers Joins.