Year-over-Year Growth Percentage
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP
Problem
Leadership wants year-over-year growth. For each year, show the previous year's sales and the percentage change between them.
Schema — `yearly_sales`
| Column | Type | | --- | --- | | sales_year | INT | | sales | INT |
Example Input — `yearly_sales`
| sales_year | sales | | --- | --- | | 2021 | 5000 | | 2022 | 6500 | | 2023 | 6000 | | 2024 | 7800 |
Expected Output
| sales_year | sales | prev_year_sales | yoy_growth_pct | | --- | --- | --- | --- | | 2021 | 5000 | NULL | NULL | | 2022 | 6500 | 5000 | 30.00 | | 2023 | 6000 | 6500 | -7.69 | | 2024 | 7800 | 6000 | 30.00 |
Explanation
2021 is the first year on record, so it has no previous year and its growth is NULL. 2023 sold less than 2022, so its growth is negative — that is correct, not an error.
Notes
- Return:
sales_year,sales,prev_year_sales,yoy_growth_pct(2 decimals) - Row order is not graded; the example is ordered for readability
- Same problem in PySpark: [Year-over-Year Growth Percentage (PySpark)](/challenges/year-over-year-growth-percentage-pyspark)
What this SQL challenge teaches you
“Year-over-Year 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
- LAG
- OVER
- Growth
- Window Functions
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:
- LAG(sales) OVER (ORDER BY sales_year) reads the previous year's row.
- Growth is (current - previous) / previous * 100.
- Multiply by 100.0, not 100, or integer division truncates it to 0.
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
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
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.