Total Sales Amount by Each Customer
SQL coding challenge · Difficulty: easy · Topic: Aggregation · +50 XP
Problem
For each customer, calculate the total sales amount (sum of amount of all their orders) from the sales table.
Tables
Table: sales
| order_id | customer_id | product_id | amount | order_date | | --- | --- | --- | --- | --- | | 101 | 1 | 11 | 1000 | 2024-01-01 | | 102 | 1 | 12 | 1500 | 2024-01-03 | | 103 | 2 | 11 | 800 | 2024-01-02 | | 104 | 2 | 13 | 450 | 2024-01-05 | | 105 | 3 | 12 | 980 | 2024-01-04 | | 106 | 4 | 13 | 760 | 2024-01-06 |
Expected Output
| customer_id | total_sales | | --- | --- | | 1 | 2500 | | 2 | 1250 | | 3 | 980 | | 4 | 760 |
- Return:
customer_id,total_sales(ordered bycustomer_id) - Approach:
SUM(amount)withGROUP BY customer_id
What this SQL challenge teaches you
“Total Sales Amount by Each Customer” is a easy-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with GROUP BY, SUM, Aggregation — 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
- GROUP BY
- SUM
- Aggregation
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:
- SUM() with GROUP BY gives one row per customer. Order the result by customer_id so it is deterministic.
- SELECT customer_id, SUM(amount) AS total_sales ... GROUP BY customer_id. The alias must be exactly total_sales.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Flipkart. 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 Duplicate Emails
- HR: Average Salary by Department
- Logistics: Count Shipments by Status
- Count Total Orders Placed by Each Customer
- Find Average Order Amount for Each Customer
- Find Customers Who Placed More Than 5 Orders (HAVING)
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 Aggregation.