How to Compute YoY and MoM with SQL

Data 4 Everyone!
2 min readDec 16, 2022

--

Calculating year-over-year (YoY) and month-over-month (MoM) growth can be crucial for businesses to track the performance of their products, services, and overall operations.

Photo by Stephen Dawson on Unsplash

With the help of SQL and the SuperStore dataset (as an example), it’s easy to calculate these metrics and better understand your company’s growth trends.

To begin, let’s first understand what YoY and MoM growth refer to:

  • YoY growth compares the current year’s performance to the same period in the previous year. This helps businesses identify long-term trends and understand how their operations have changed.
  • On the other hand, MoM growth compares the current month’s performance to the previous month, providing a more short-term perspective on how the business is doing.

Now, let’s learn how to calculate these metrics using SQL and the SuperStore dataset.

Let’s start with YoY growth for sales.

To do this, you’ll need to divide the current year’s sales by the previous year’s sales and multiply by 100. You can do this using the following SQL code:

SELECT
((sales/LAG(sales,1) OVER (ORDER BY year))*100) AS YoY
FROM
SuperStore
WHERE
year IN (YEAR(CURRENT_DATE()), YEAR(CURRENT_DATE())-1)

This will give you the dataset's YoY growth rate for each year. You can then use this data to track the overall trend in your business's sales over time.

Calculating MoM growth is similar, with the main difference being that you’ll be comparing the current month’s sales to the previous month’s sales instead of the current year’s sales to the previous year’s sales.

You can do this using the following SQL code:

SELECT
((sales/LAG(sales,1) OVER (ORDER BY month))*100) AS MoM
FROM
SuperStore
WHERE
month IN (MONTH(CURRENT_DATE()), MONTH(CURRENT_DATE())-1)

This will give you the dataset's MoM growth rate for each month. You can then use this data to track the short-term trends in your business's sales and make informed decisions about future actions.

In conclusion, calculating YoY and MoM growth using SQL is a quick and easy way to track the performance of your business and identify trends over time.

By using the techniques outlined above, you’ll better understand your company’s growth and make informed decisions about how to move forward.

Content generated using ChatGPT and reviewed by Mickaël Andrieu

--

--