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)
| month | revenue |
|---|---|
| 2024-01-01 | 4200 |
| 2024-02-01 | 3900 |
| 2024-03-01 | 4600 |
| 2024-05-01 | 5100 |
| 2024-06-01 | 4800 |
| 2024-07-01 | 5300 |
| 2024-08-01 | 5000 |
| 2024-09-01 | 5600 |
| 2024-10-01 | 6100 |
| 2024-11-01 | 7400 |
| 2024-12-01 | 8800 |
| 2025-01-01 | 4830 |
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 exerciseMore practice on the same topic
More exercises in Advanced
- Above the average salary
- Rank within department
- Running revenue total
- Running total per customer
- Top earner per department
- Second highest salary
- Big spenders
- Levels in the reporting chain
- Dense rank
- Change from the previous order
- Days until the next order
- Moving average
- Share of revenue
- Share of the customer's total
- Each customer's first order
- Month over month
- Top two per category
- Orders above the customer's average
- Compared to the first order
- Salary quartiles
- Cumulative share of revenue
- Latest order per customer (DISTINCT ON)
- Two largest paid orders per customer (LATERAL)
- Most orders, ties included
- Last event per user, with a tie-break
- Price at the time of the order
- Read fields out of JSON
- One row per array element