Sales Data Analysis
PYSPARK coding challenge · Difficulty: medium · Topic: Aggregation · +100 XP
A retail team keeps every order in a single table and wants one summary row
per region: how busy the region was, how many units it moved, and which product
led it.
sales_data holds one row per order.
Tables
| column | type | description | | --- | --- | --- | | order_id | INT | unique id of the order | | product | VARCHAR | product sold | | region | VARCHAR | region the order was placed in | | quantity | INT | number of units sold |
For every region, return exactly one row containing:
- region
- order_count — how many orders were placed in that region
- total_quantity — total units sold in that region
- top_product — the product with the highest total quantity in that
region. A product's total is the sum of its units across all of that
region's orders, so the product appearing in the most orders is not
necessarily the leader.
Tie-break: if two products in the same region have the same total quantity,
return the one whose name comes first alphabetically.
Each region is ranked on its own — a product that leads overall need not lead
in any particular region.
Sort the result by region ascending.
Example input
sales_data
| order_id | product | region | quantity | | --- | --- | --- | --- | | 1 | Laptop | East | 2 | | 2 | Mobile | West | 5 | | 3 | Laptop | East | 1 | | 4 | Tablet | North | 3 | | 5 | Mobile | West | 2 |
Expected output
| region | order_count | total_quantity | top_product | | --- | --- | --- | --- | | East | 2 | 3 | Laptop | | North | 1 | 3 | Tablet | | West | 2 | 7 | Mobile |
East placed 2 orders totalling 3 units, all of them Laptop. West placed 2
orders totalling 7 units, both Mobile.
What this PYSPARK challenge teaches you
“Sales Data Analysis” is a medium-level PYSPARK challenge focused on Aggregation. Working through it gives you hands-on practice with aggregation, group-by, window-functions — 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
- aggregation
- group-by
- window-functions
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:
- Two different groupings are needed: one per region for the counts, and one per region AND product to find the leader. Work them out separately, then join them.
- ROW_NUMBER() OVER (PARTITION BY region ORDER BY SUM(quantity) DESC, product ASC) ranks products inside each region and settles ties in one step.
- COUNT(*) counts ORDERS, not units. total_quantity is SUM(quantity), and top_product is decided by summed quantity too -- not by how many rows a product appears in.
Where this comes up
Variations of this problem have been reported in interviews at Deloitte. 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 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
- 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)
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 Aggregation.