Latest Order Per Customer
SQL coding challenge · Difficulty: easy · Topic: Window Functions · +50 XP
Problem
For each customer, find the date of their most recent order.
Every customer that appears in the table must appear exactly once in the output.
Tables
Table: orders
| Column | Type | | --- | --- | | order_id | INT | | customer_id | INT | | order_date | DATE | | total_amount | DECIMAL |
Example Input
| order_id | customer_id | order_date | total_amount | | --- | --- | --- | --- | | 1 | 201 | 2024-03-05 | 150.00 | | 2 | 201 | 2024-03-18 | 200.00 | | 3 | 202 | 2024-02-25 | 300.00 | | 4 | 203 | 2024-03-10 | 450.00 | | 5 | 203 | 2024-01-20 | 120.00 | | 6 | 203 | 2024-03-22 | 175.00 |
Expected Output
| customer_id | latest_order_date | | --- | --- | | 201 | 2024-03-18 | | 202 | 2024-02-25 | | 203 | 2024-03-22 |
Explanation
Customer 201 ordered twice; the later date is 2024-03-18.
Customer 203 ordered three times and the latest is 2024-03-22 — note the rows are not stored in date order, so you cannot simply take the last row per customer.
Notes
- Return:
customer_id,latest_order_date - Sort by
customer_id ASC - Only those two columns are graded — do not return
order_idortotal_amount
What this SQL challenge teaches you
“Latest Order Per Customer” is a easy-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with ROW_NUMBER, window functions, date — 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
- ROW_NUMBER
- window functions
- date
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:
- Your result should return: customer_id, order_id, order_date,.
- Compare your output to the Expected Output — the columns, values and row order must match exactly.
- ROW_NUMBER() OVER ( PARTITION BY customer_id ORDER BY order_date DESC, order_id DESC )
Where this comes up
Variations of this problem have been reported in interviews at Flipkart, Amazon, Meesho. 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 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
- Top 3 Products per Category
- Running Total Revenue
- Median Salary per Department
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
- Rolling Sum with Missing Dates
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 Window Functions.