Number of Orders by Each Customer per Month
SQL coding challenge · Difficulty: medium · Topic: Aggregation · +75 XP
Problem
Count how many orders each customer placed in each month. Group the month down to its first day (e.g. 2024-01-01).
Note: MySQL has no DATE_TRUNC — use DATE_FORMAT(order_date, '%Y-%m-01') to get the month.
Tables
Table: sales
| order_id | customer_id | product_id | amount | order_date | | --- | --- | --- | --- | --- | | 201 | 1 | 11 | 100 | 2024-01-05 | | 202 | 1 | 12 | 200 | 2024-01-20 | | 203 | 1 | 11 | 150 | 2024-02-10 | | 204 | 2 | 13 | 300 | 2024-01-15 | | 205 | 2 | 12 | 250 | 2024-02-05 | | 206 | 2 | 11 | 400 | 2024-02-25 |
Expected Output
| customer_id | month | order_count | | --- | --- | --- | | 1 | 2024-01-01 | 2 | | 1 | 2024-02-01 | 1 | | 2 | 2024-01-01 | 1 | | 2 | 2024-02-01 | 2 |
- Return:
customer_id,month,order_count(ordered bycustomer_id,month) - Approach:
COUNT(*)withGROUP BY customer_id, DATE_FORMAT(order_date,'%Y-%m-01')
What this SQL challenge teaches you
“Number of Orders by Each Customer per Month” is a medium-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with GROUP BY, COUNT, DATE_FORMAT, 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
- GROUP BY
- COUNT
- DATE_FORMAT
- 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:
- Two customers can order in the same month, so you need to group by BOTH the customer and the month.
- DATE_FORMAT(order_date, '%Y-%m-01') collapses every date in a month to its first day. Group by that expression, not by order_date.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Netflix, DataDog. 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
- 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 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 medium and covers Aggregation.