KatInterview

Calculating Monthly Theme Engagement and Revenue Trends in SQL

SQL & DataModel answer
MediumModel answerSQL & Datasqltime-seriesaggregation

Last updated

Define usage growth metrics and write SQL to analyze monthly active users, new user acquisition, and discrete monthly revenue for custom app themes.

You are analyzing user engagement and monetization data for a custom theme platform. You are given two tables capturing user interactions and purchases for specific digital themes.

Schema

theme_usage

+-----------+------------+-------------+
| user_id   | theme_name | created_at  |
+-----------+------------+-------------+
| 101       | pirate     | 2024-01-05  |
| 102       | pirate     | 2024-01-06  |
| 101       | pirate     | 2024-02-14  |
| 110       | pirate     | 2024-02-20  |
| 103       | pirate     | 2024-03-01  |
+-----------+------------+-------------+

theme_transactions

+-----------+------------+--------+
| order_id  | created_at | amount |
+-----------+------------+--------+
| 1         | 2024-01-05 | 2.99   |
| 2         | 2024-01-06 | 2.99   |
| 3         | 2024-02-14 | 2.99   |
| 4         | 2024-02-20 | 2.99   |
| 5         | 2024-03-01 | 2.99   |
+-----------+------------+--------+

Objective

  1. Propose a meaningful metric to quantify month-over-month (MoM) growth in usage for the pirate theme. Write a SQL query that generates this metric aggregated by calendar month, alongside the percentage growth rate relative to the preceding month.
  2. Write a SQL query to calculate non-cumulative monthly revenue and transaction count specifically for the pirate theme (assume theme_transactions contains sales data for this theme), along with month-over-month revenue growth percentage.

Hints

Hint 1

Avoid using raw event counts as your primary usage metric, as power users can skew the trend. Consider Monthly Active Users (MAU) using COUNT(DISTINCT user_id).

Hint 2

Use DATE_TRUNC('month', created_at) to aggregate timestamps into calendar months.

Hint 3

To correctly calculate month-over-month growth when certain months might have zero activity, generate a complete series of continuous monthly dates.

Hint 4

Use NULLIF when calculating percentage changes to prevent division-by-zero runtime errors.