Loading problem...
An e-commerce analytics platform tracks user profiles and purchase events.
Table: customers
Table: orders
Business rule:
Task: For every customer who has placed at least one order, return their three most recent orders. If a customer has fewer than three orders, return all available orders for that customer.
Output requirements:
Supported submission environments:
customers:
| customer_id | name |
|-------------|-----------|
| 1 | Winston |
| 2 | Jonathan |
| 3 | Annabelle |
| 4 | Marwan |
| 5 | Khaled |
orders:
| order_id | order_date | customer_id | cost |
|----------|-------------|-------------|------|
| 1 | 2020-07-31 | 1 | 30 |
| 2 | 2020-07-30 | 2 | 40 |
| 3 | 2020-07-31 | 3 | 70 |
| 4 | 2020-07-29 | 4 | 100 |
| 5 | 2020-06-10 | 1 | 1010 |
| 6 | 2020-08-01 | 2 | 102 |
| 7 | 2020-08-01 | 3 | 111 |
| 8 | 2020-08-03 | 1 | 99 |
| 9 | 2020-08-07 | 2 | 32 |
| 10 | 2020-07-15 | 1 | 2 |[
{"customer_name":"Annabelle","customer_id":3,"order_id":7,"order_date":"2020-08-01"},
{"customer_name":"Annabelle","customer_id":3,"order_id":3,"order_date":"2020-07-31"},
{"customer_name":"Jonathan","customer_id":2,"order_id":9,"order_date":"2020-08-07"},
{"customer_name":"Jonathan","customer_id":2,"order_id":6,"order_date":"2020-08-01"},
{"customer_name":"Jonathan","customer_id":2,"order_id":2,"order_date":"2020-07-30"},
{"customer_name":"Marwan","customer_id":4,"order_id":4,"order_date":"2020-07-29"},
{"customer_name":"Winston","customer_id":1,"order_id":8,"order_date":"2020-08-03"},
{"customer_name":"Winston","customer_id":1,"order_id":1,"order_date":"2020-07-31"},
{"customer_name":"Winston","customer_id":1,"order_id":10,"order_date":"2020-07-15"}
]Customer 1 has four orders, so only the three most recent dates are retained. Customers with fewer than three orders contribute all of their rows.
customers:
| customer_id | name |
|-------------|-------|
| 10 | Asha |
| 11 | Asha |
| 12 | Bruno |
orders:
| order_id | order_date | customer_id | cost |
|----------|-------------|-------------|------|
| 101 | 2024-01-03 | 10 | 80 |
| 102 | 2024-01-08 | 10 | 81 |
| 103 | 2024-01-12 | 11 | 90 |
| 104 | 2024-01-01 | 12 | 70 |[
{"customer_name":"Asha","customer_id":10,"order_id":102,"order_date":"2024-01-08"},
{"customer_name":"Asha","customer_id":10,"order_id":101,"order_date":"2024-01-03"},
{"customer_name":"Asha","customer_id":11,"order_id":103,"order_date":"2024-01-12"},
{"customer_name":"Bruno","customer_id":12,"order_id":104,"order_date":"2024-01-01"}
]When names are equal, customer_id is the tie-break key. Each customer is still processed independently for their top three recent orders.
customers:
| customer_id | name |
|-------------|--------|
| 21 | Irene |
| 22 | Jasper |
orders:
| order_id | order_date | customer_id | cost |
|----------|-------------|-------------|------|
| 201 | 2024-05-01 | 21 | 10 |
| 202 | 2024-05-02 | 21 | 12 |
| 203 | 2024-05-03 | 21 | 14 |
| 204 | 2024-05-04 | 21 | 16 |[
{"customer_name":"Irene","customer_id":21,"order_id":204,"order_date":"2024-05-04"},
{"customer_name":"Irene","customer_id":21,"order_id":203,"order_date":"2024-05-03"},
{"customer_name":"Irene","customer_id":21,"order_id":202,"order_date":"2024-05-02"}
]Customers without orders are absent from the result. For customer 21, the oldest order is dropped to keep only the latest three.
Constraints