Plan changes

window functions

Plan changes

Shopify SQL Interview Question

An upgrade and a downgrade at Shopify go to different teams. An upgrade is a success story the account team follows up on, and a downgrade is a churn risk somebody should call this week. Both are invisible in a billing table until each month is read beside the month before it.

Return every month a merchant was on a different plan than the record before: the merchant_id, the active_month the change took effect, the plan they were on before as previous_plan, and the plan they moved to as new_plan. Sort by merchant_id and then by active_month.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

shopify_subscriptionsTable26 rows

Column NameType
record_idBIGINT
merchant_idVARCHAR
active_monthDATE
planVARCHAR
mrrDOUBLE

shopify_subscriptionsExample Input

record_idmerchant_idactive_monthplanmrr
1101M012024-01-01Basic29
1102M012024-02-01Basic29
1103M012024-03-01Shopify79
1104M012024-06-01Shopify79
1105M012024-07-01Advanced299
1106M012024-08-01Advanced299
1119M042024-01-01Advanced299
1120M042024-02-01Advanced299
1121M042024-03-01Advanced299
1122M042024-04-01Advanced299

Example Output

merchant_idactive_monthprevious_plannew_plan
M012024-03-01BasicShopify
M012024-07-01ShopifyAdvanced

Explanation

M01 moved up from Basic in March and up again in July. Their first month is not a change, since there is no earlier plan to have moved from. M04 stayed on one plan throughout and never appears.

The example above is a small slice of the data. Your query runs against the full tables.