Month-over-Month Average Order Value
SQL coding challenge · Difficulty: medium · Topic: Aggregation · +100 XP
Problem
Average order value (AOV) is total revenue divided by number of orders. Show it per month, alongside the previous month's AOV and the change between them.
Schema — `monthly_orders`
| Column | Type | | --- | --- | | order_month | VARCHAR(7) | | amount | INT |
Example Input — `monthly_orders`
| order_month | amount | | --- | --- | | 2024-01 | 100 | | 2024-01 | 200 | | 2024-02 | 300 | | 2024-02 | 100 | | 2024-02 | 200 | | 2024-03 | 50 | | 2024-03 | 150 |
Expected Output
| order_month | avg_order_value | prev_aov | aov_change | | --- | --- | --- | --- | | 2024-01 | 150.00 | NULL | NULL | | 2024-02 | 200.00 | 150.00 | 50.00 | | 2024-03 | 100.00 | 200.00 | -100.00 |
Explanation
This needs two steps: aggregate the orders into one row per month, then compare each month with the one before it. January has no previous month, so its comparison columns are NULL. A month can have more orders but a lower AOV — February here sells more in total than January, yet its AOV is what matters.
Notes
- Return:
order_month,avg_order_value,prev_aov,aov_change(2 decimals) - Row order is not graded; the example is ordered for readability
- Same problem in PySpark: [Month-over-Month Average Order Value (PySpark)](/challenges/month-over-month-average-order-value-pyspark)
What this SQL challenge teaches you
“Month-over-Month Average Order Value” is a medium-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with CTE, GROUP BY, LAG, 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
- CTE
- GROUP BY
- LAG
- 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:
- Aggregate first: GROUP BY order_month to get one row per month.
- A CTE (WITH ...) lets you apply LAG to the aggregated rows.
- LAG cannot see rows you have already collapsed with GROUP BY, which is why the aggregation has to happen first.
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)
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.