Month-over-Month Growth Percentage

SQL coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP

Problem

The business wants monthly growth as a percentage of the previous month. January has no previous month, so its growth is NULL. A fall in sales gives a negative percentage.

Schema — `monthly_sales`

| Column | Type |
| --- | --- |
| month_start | DATE |
| sales | INT |

Example Input — `monthly_sales`

| month_start | sales |
| --- | --- |
| 2024-01-01 | 1000 |
| 2024-02-01 | 1200 |
| 2024-03-01 | 1500 |
| 2024-04-01 | 1300 |

Expected Output

| month_start | sales | prev_month_sales | mom_growth_pct |
| --- | --- | --- | --- |
| 2024-01-01 | 1000 | NULL | NULL |
| 2024-02-01 | 1200 | 1000 | 20.00 |
| 2024-03-01 | 1500 | 1200 | 25.00 |
| 2024-04-01 | 1300 | 1500 | -13.33 |

Explanation

January has no previous month, so prev_month_sales and mom_growth_pct are NULL.

February sold 1200 against January's 1000: (1200 - 1000) / 1000 * 100 = 20.00. March sold 1500 against 1200: (1500 - 1200) / 1200 * 100 = 25.00. April sold 1300 against March's 1500, so growth is negative: (1300 - 1500) / 1500 * 100 = -13.33.

Multiply by 100.0 rather than 100 — with integer arithmetic the division truncates to 0 before the multiplication ever happens.

Notes

What this SQL challenge teaches you

“Month-over-Month Growth Percentage” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with LAG, OVER, Growth, Window Functions — 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. Growth is (current - previous) / previous * 100.
  2. Multiply by 100.0, not 100, or integer division truncates the result.
  3. The first month has no previous month, so it stays NULL.

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 medium and covers Window Functions.

Solve this challenge free on PySpark.in