Calculate Total Sales and Average Order Value by Customer
PYSPARK coding challenge · Difficulty: medium · Topic: GroupBy Aggregations · +100 XP
Problem
For each customer_id, compute:
total_sales— the sum oforder_valueavg_order_value— the average oforder_value
Return a DataFrame with columns customer_id, total_sales, avg_order_value.
Your Task
For each customer, compute the sum and the average of their order values.
Example Input - `orders`
| order_id | customer_id | order_value | | --- | --- | --- | | 1 | 101 | 250.00 | | 2 | 102 | 300.00 | | 3 | 101 | 150.00 | | 4 | 103 | 400.00 | | 5 | 102 | 200.00 | | 6 | 101 | 350.00 |
Expected Output
| customer_id | total_sales | avg_order_value | | --- | --- | --- | | 101 | 750.0 | 250.0 | | 102 | 500.0 | 250.0 | | 103 | 500.0 | 250.0 |
Implementation Notes
- The DataFrame is created for you - do not recreate it
- Build a DataFrame called
df_resultand finish withdf_result.show()
Constraints
- Between 1 and 1,000,000 rows.
- Each customer_id appears between 1 and 50 times.
- order_value is a float greater than 0.
- Use the DataFrame API rather than SQL strings.
Success Criteria
- Your DataFrame has customer_id, total_sales and avg_order_value.
- Exactly one row comes back per customer_id.
- total_sales is the sum of order_value; avg_order_value is its mean, not total divided by a row count you chose.
- Two customers can share an average while having different totals -- both must still be right.
- You finish by calling .show() on the result, which is what the grader reads.
What this PYSPARK challenge teaches you
“Calculate Total Sales and Average Order Value by Customer” is a medium-level PYSPARK challenge focused on GroupBy Aggregations. Working through it gives you hands-on practice with groupBy, agg, avg, sum — 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
- groupBy
- agg
- avg
- sum
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:
- One row per customer means grouping by customer_id -- the two figures are then two aggregates over the same group.
- agg() takes more than one aggregate at a time: F.sum('order_value') and F.avg('order_value') in the same call.
- Name them with .alias('total_sales') and .alias('avg_order_value'), because the grader checks the column names.
How to practise it on PySpark.in
Open the challenge, write your PySpark code 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 PYSPARK challenges
- Analyze Sales Data to Find Top Performers by Region
- Count Rows per City
- Analyze Product Sales by Region (PySpark)
- Fix the Broken Pipeline
- Optimize the 100M-Row Join
- Parse Apache Logs with Regex
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYSPARK 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 medium and covers GroupBy Aggregations.