Products with Review Growth
For each product, find its largest month-over-month increase in the number of reviews.
The challenge runs on the SQLabHub practice database (SQLite) and uses the reviews table. You can solve it in the browser, with nothing to install.
The task
For each product, find its largest month-over-month increase in the number of reviews.
A correct answer returns 96 rows with 2 columns: product_id, growth.
Tables used in this challenge
reviews 192 rows
id INTEGER (PK) · product_id INTEGER · user_id INTEGER · rating INTEGER · comment TEXT · reviewed_at TEXT · helpful_votes INTEGER
| id | product_id | user_id | rating | comment | reviewed_at | helpful_votes |
|---|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| product_id | growth |
|---|---|
First 5 of 96 rows. This is the output of the model solution on the practice database.
What you practise
- Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
- Grouping rows with GROUP BY to get one result row per group
- Grouping by more than one column to get a row per combination
- Querying the result of another query with a subquery in FROM
- Working with dates in SQLite: strftime, julianday and DATE
- Reading the previous row's value with LAG()
- Restarting a window calculation for each group with PARTITION BY
- Sorting the result with ORDER BY
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
Count reviews per product and month, LAG within the product, then MAX of the difference
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Build an inner query first (a derived table):
- Build an inner query first (a derived table):
- Read from the
reviewstable. - Group the rows by
product_idandmonth(the year-month ofreviewed_at), so the result has one row for each combination. - For each group, compute
COUNT(*).
- Read from the
- Fetch
cntfrom the previous row withLAG(), ordered bymonth, restarting for eachproduct_id. Where there is no such row the value is NULL.
- Build an inner query first (a derived table):
- Group the rows by
product_id, so the result has one row for each value. - For each group, compute
MAX(cnt - prev). - Sort the result by
growthdescending.
Show the model solution
SELECT product_id, MAX(cnt - prev) AS growth FROM (
SELECT product_id, cnt, LAG(cnt) OVER (PARTITION BY product_id ORDER BY month) AS prev
FROM (
SELECT product_id, strftime('%Y-%m', reviewed_at) AS month, COUNT(*) AS cnt
FROM reviews GROUP BY product_id, month
)
) GROUP BY product_id ORDER BY growth DESC;This query produced the expected output above. Yours does not have to match it word for word: any query that returns the same rows is correct.
What to watch for
- LAG(). LAG() returns NULL for the first row of each partition, and any arithmetic with that NULL is NULL too. Supply a default as the third argument if you need a number.
- PARTITION BY. PARTITION BY is not GROUP BY: it keeps every row and only decides which rows a window function looks at.
- Derived table. Columns computed in the inner query are only visible outside under their alias, so every calculated column there needs a name.
Related SQL challenges
- Calculate the Average Days Between Orders per CustomerSimilar levelHard
- Calculate Month-over-Month Revenue Change per ProductSimilar levelHard
- Show Review Count and Units Sold per ProductEasierMedium
- Calculate Week-over-Week Revenue ChangeSimilar levelHard
- Measure Monthly Revenue Volatility per ProductSimilar levelHard
Keep learning
- Lesson: LAG & LEAD
- Lesson: Window Functions
- Lesson: SQL Subqueries
- Lesson: GROUP BY Multiple Columns
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database