Urgent.News

What's breaking now, across thousands of outlets.

Tech

Month-over-Month Growth in SQL: LAG, the Growth Formula, and the Traps

By Michael Nocito , data analyst · Published August 7, 2026 By the end of this page you can write a month-over-month growth query and trust its answer. You will know the growth formula and how to say it in a sentence, what LAG() actually does, and the three traps that produce wrong percentages without producing an error: integer division, the empty first month, and the missing month. It is about…

Month-over-month growth in SQL is a crucial concept that can be calculated using the LAG function. This guide will walk you through the process, explain how LAG works, and highlight three common traps that can lead to incorrect percentage calculations. To ensure accuracy, always verify your data first by counting distinct months and comparing it to the calendar.

If any month is missing, your growth numbers may be comparing the wrong pairs of months. The key formula for month-over-month growth is: this month's revenue minus last month's revenue, divided by last month's revenue. This turns raw numbers into a share of the starting value, making the growth rate comparable across different scales.

Remember, "month over month" always refers to comparing the current month with the immediately preceding month, not with the same month in the previous year, which would be "year over year." Before performing any comparisons, ensure your data is in a single row per month, achieved through grouping by month and summing the amounts.

Use LAG(revenue) OVER (ORDER BY month) to fetch the previous month's revenue onto the current month's row. This function sorts the rows by month and then provides the revenue from the row above it, enabling you to calculate the growth rate accurately.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

How to Find Duplicate Rows in SQL (and Decide What Counts as One)

By Michael Nocito , data analyst · Published August 7, 2026 By the end of this page you can check any table for duplicate rows, list every copy, and mark which one to keep, all with queries you…

  • Decide what defines a duplicate (all columns or specific column)
  • Compare total row count to distinct key count using COUNT() vs COUNT(DISTINCT key)
  • Group rows by duplicate columns, keep groups with COUNT() > 1, mark extras with ROWNUMBER

pandas GroupBy: How to Summarize a DataFrame Without Losing Track of Your Rows

By Michael Nocito , data analyst · Published August 7, 2026 By the end of this page you can take a DataFrame, summarize it by any column or combination of columns, get several statistics at once with…

  • Groupby operation splits DataFrame into mini-tables based on unique values in grouping columns
  • Basic usage: df.groupby(region)[amount].mean() computes mean of 'amount' column for each region
  • Multiple statistics simultaneously using agg and named aggregation for cleaner output

NULL in SQL: Why = NULL Finds Nothing and What to Write Instead

By Michael Nocito , data analyst · Published August 7, 2026 By the end of this page you can predict what any query does when it meets a missing value, which is the skill that separates "my query…

  • NULL represents unknown or missing values in SQL
  • Comparing with = NULL always returns unknown and excludes rows
  • IS NULL operator checks specifically for NULL values

Which SQL Database Should You Install?

By Michael Nocito , data analyst · Published August 7, 2026 If you want to write SQL against your own data today, install a file-based engine: SQLite if you are learning, DuckDB if your data is…

  • File-based databases like SQLite or DuckDB recommended for single-user or small projects
  • Server databases such as PostgreSQL, MySQL, or SQL Server needed for multiple users
  • Choose based on whether simultaneous access to data is required

More from Friday 21 August →