cswork / libraryOpen practice workspace

Coding questions · Shopify

Determine Growth of Pirated Theme Installations Over Years

This question evaluates proficiency in SQL-based data manipulation and analytics, focusing on computing time-based cohort percentages and conducting year-over-year trend analysis of merchant behavior.

merchants

+-------------+------------+
| merchant_id | join_date  |
+-------------+------------+
| 1           | 2020-01-02 |
| 2           | 2020-03-11 |
| 3           | 2021-05-20 |
| 4           | 2021-07-15 |
| 5           | 2022-02-22 |
+-------------+------------+

themes

+----------+------------+
| theme_id | is_pirated |
+----------+------------+
| 101      | TRUE       |
| 102      | FALSE      |
| 103      | TRUE       |
| 104      | FALSE      |
| 105      | TRUE       |
+----------+------------+

merchant_themes

+-------------+----------+------------+
| merchant_id | theme_id | install_dt |
+-------------+----------+------------+
| 1           | 101      | 2020-01-02 |
| 1           | 102      | 2020-06-01 |
| 2           | 103      | 2021-02-10 |
| 3           | 104      | 2021-07-01 |
| 4           | 105      | 2022-03-11 |
+-------------+----------+------------+
Scenario

Shopify wants to understand if the proportion of merchants installing pirated storefront themes is growing over time.

Question

Write a SQL query that returns, for each calendar year, the percentage of active merchants who installed at least one pirated theme. From this result, determine whether adoption of pirated themes is increasing year-over-year.

Hints

Join theme installations to theme attributes, aggregate distinct merchant_ids by year, then compare year-over-year percentages.

Yearly Percentage of Merchants Installing Pirated Themes

Using the tables below, write a SQL query that returns, for each calendar year, the percentage of active merchants (those with at least one theme installation that year) who installed at least one pirated theme. Then, based on the result, determine whether adoption of pirated themes is increasing year-over-year.

Table merchants

columntypeconstraints
merchant_idINTEGERPRIMARY KEY, NOT NULL
join_dateDATENOT NULL

Sample data:

join_datemerchant_id
2020-01-021
2020-03-112
2021-05-203
2021-07-154
2022-02-225

Table themes

columntypeconstraints
theme_idINTEGERPRIMARY KEY, NOT NULL
is_piratedBOOLEANNOT NULL

Sample data:

is_piratedtheme_id
true101
false102
true103
false104
true105

Table merchant_themes

columntypeconstraints
merchant_idINTEGERNOT NULL, FOREIGN KEY REFERENCES merchants(merchant_id)
theme_idINTEGERNOT NULL, FOREIGN KEY REFERENCES themes(theme_id)
install_dtDATENOT NULL

Sample data:

install_dtmerchant_idtheme_id
2020-01-021101
2020-06-011102
2021-02-102103
2021-07-013104
2022-03-114105

Expected output

Active merchants are defined as those with at least one theme installation in the given year. For each year, compute the percentage as: (distinct merchants with ≥1 pirated install that year) / (distinct merchants with any install that year) * 100. From the sample data: in 2020, merchant 1 installs a pirated theme (100%); in 2021, merchants 2 and 3 are active but only merchant 2 installs a pirated theme (50%); in 2022, merchant 4 is active and installs a pirated theme (100%). Adoption is not consistently increasing year-over-year; it decreases from 2020 to 2021, then increases again in 2022.

active_merchantsmerchants_with_pirated_installpct_piratedyear
111002020
21502021
111002022

Hints

  1. Join theme installations (merchant_themes) to theme attributes (themes) to identify pirated installs.
  2. Aggregate to a per-merchant-per-year level to avoid double-counting merchants with multiple installs in a year.
  3. Compute the yearly percentage as merchants_with_pirated_install divided by active_merchants.