Querydrill

Level 4: Advanced Exercise 96 of 161 Medium Dates and periods

Year-over-year growth

An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.

Try it yourself

For every month of 2025 that is in the table, return the month, its revenue, the revenue of the same month one year earlier, and the growth as a percentage rounded to 1 decimal. When there is no figure for the same month a year earlier, the last two columns are NULL. When that figure is 0, the growth is NULL.

monthly_revenue (first 12 of 17 rows)

monthrevenue
2024-01-014200
2024-02-013900
2024-03-014600
2024-05-015100
2024-06-014800
2024-07-015300
2024-08-015000
2024-09-015600
2024-10-016100
2024-11-017400
2024-12-018800
2025-01-014830

Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.

What this exercise teaches

A self join on the date one year earlier compares each month with the right month, also when a month is missing from the table. LAG(revenue, 12) counts rows, not months, so one missing month shifts every comparison after it. NULLIF guards the division against a zero.

A hint if you get stuck

Join the table to itself on month minus one year. Do not count 12 rows back: a month can be missing.

Background you need

Common mistakes on this exercise

  • LAG counts rows, not months: one missing month and every comparison is with the wrong month.
  • The inner join drops a 2025 month that has no figure a year earlier.

Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.

Keep going on the practice page

Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.

Open this exercise

More practice on the same topic

More exercises in Advanced