First Order Date per Customer (PySpark)
PYSPARK coding challenge · Difficulty: easy · Topic: Aggregation · +50 XP
Problem
For a cohort analysis you need each customer's very first order date, along with how many orders they have placed in total.
Schema — `customer_orders`
| Column | | --- | | customer_id | | order_date | | amount |
Example Input — `customer_orders`
| customer_id | order_date | amount | | --- | --- | --- | | 1 | 2024-06-01 | 100 | | 1 | 2024-06-04 | 150 | | 2 | 2024-06-02 | 200 | | 2 | 2024-06-05 | 120 | | 3 | 2024-06-03 | 300 |
Expected Output
| customer_id | first_order_date | order_count | | --- | --- | --- | | 1 | 2024-06-01 | 2 | | 2 | 2024-06-02 | 2 | | 3 | 2024-06-03 | 1 |
Explanation
One row per customer, not one row per order — customer 1 ordered on 2024-06-01 and 2024-06-04, so they appear once, with a first order date of 2024-06-01 and an order count of 2.
Notes
- The DataFrame is created for you — do not recreate it
- Build a DataFrame called
df_resultand finish withdf_result.show() - Same problem in SQL: [First Order Date per Customer](/challenges/first-order-date-per-customer)
What this PYSPARK challenge teaches you
“First Order Date per Customer (PySpark)” is a easy-level PYSPARK challenge focused on Aggregation. Working through it gives you hands-on practice with groupBy, agg, min, 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
- groupBy
- agg
- min
- 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:
- groupBy('customer_id') then .agg(...) with two aggregates.
- F.min('order_date').alias('first_order_date')
- F.count('*').alias('order_count')
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)
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 easy and covers Aggregation.