First Order Date per Customer

SQL 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 | Type |
| --- | --- |
| customer_id | INT |
| order_date | DATE |
| amount | INT |

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

What this SQL challenge teaches you

“First Order Date per Customer” is a easy-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with GROUP BY, MIN, COUNT, 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

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:

  1. GROUP BY customer_id collapses each customer to a single row.
  2. MIN(order_date) is the earliest order for that customer.
  3. COUNT(*) counts the rows inside each group.

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

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.

Solve this challenge free on PySpark.in