Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Plan Upgrade Detection
00:00
5 left

SQL Plan Upgrade Detection

MediumSQL · PostgreSQL

Problem

Given a relational schema representing user subscriptions, write a SQL query to identify customers who upgraded their plans twice within a single month.

Use the customers, subscription_changes, and plans tables. An upgrade occurs when the new plan has a higher rank than the previous plan. Include customers with at least two upgrades in the same calendar month.

Output

  1. One row per qualifying customer and calendar month
  2. Columns: customer_id, customer_name, upgrade_month, and upgrade_count
  3. Sort by upgrade_month ascending, then customer_id ascending

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
regionVARCHAR(50)Customer billing region
subscription_changes
ColumnTypeDescription
change_idPKINTUnique subscription change identifier
customer_idINTCustomer affected by the change
old_plan_idINTPlan before the change, null for an initial subscription
new_plan_idINTPlan after the change
changed_atTIMESTAMPTimestamp when the plan change occurred
plans
ColumnTypeDescription
plan_idPKINTUnique plan identifier
plan_nameVARCHAR(50)Plan display name
plan_rankINTOrdinal level used to compare plan tiers
monthly_priceDECIMAL(10,2)Monthly list price
Tablescustomerssubscription_changesplans
Interviewer

Your question is SQL Plan Upgrade Detection. Start with the requirements and the three tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.