HardSQL challenge #282WINDOW FUNCTIONAGGREGATIONTREND

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.

Solve This SQL Challenge

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

First 3 rows of the reviews table
idproduct_iduser_idratingcommentreviewed_athelpful_votes
1321365Great product, exactly as described. Fast shipping!2023-03-1541
2301401Slightly disappointed, expected more for the price.2023-06-234
3311405Excellent build quality. Will buy again!2023-07-076

The first rows of each table are shown. See how all the tables relate on the database schema page.

Expected output

Expected output of the challenge
product_idgrowth
841
761
221
181
1190

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:

  1. Build an inner query first (a derived table):
    1. Build an inner query first (a derived table):
      1. Read from the reviews table.
      2. Group the rows by product_id and month (the year-month of reviewed_at), so the result has one row for each combination.
      3. For each group, compute COUNT(*).
    2. Fetch cnt from the previous row with LAG(), ordered by month, restarting for each product_id. Where there is no such row the value is NULL.
  2. Group the rows by product_id, so the result has one row for each value.
  3. For each group, compute MAX(cnt - prev).
  4. Sort the result by growth descending.
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.
Solve This SQL Challenge

Keep learning