Calculate MRR from a subscriptions table using SQL

Tests your ability to translate a business metric (MRR) into a precise SQL query. A great answer filters for active subscriptions this month and sums their prices, correctly amortizing annual plans.
What's really being asked
This question tests your ability to translate a core business metric, Monthly Recurring Revenue (MRR), into a precise SQL query. Interviewers are evaluating your SQL proficiency with date functions and conditional logic, your understanding of subscription data models, and your ability to reason about business logic and edge cases, like handling different plan types (monthly vs. annual).
The full answer
First, a clear definition of an 'active' subscription for the given month. This is any subscription that started on or before the last day of the month AND ends after the first day of the month (or has a null end date). Second, a SQL query that implements this logic in the WHERE clause. Third, the correct aggregation, which is a SUM of prices. Fourth, for the follow-up, it uses a CASE statement to handle different plan types, typically dividing an annual price by 12. A top-tier answer also asks clarifying questions about the schema, like whether the monthly_price for an annual plan is the total annual cost and how to handle null end_date values.
The mistakes people make
A major red flag is writing a WHERE clause that only captures subscriptions starting in the current month (WHERE start_date BETWEEN '...' AND '...'). This calculates new MRR, not total MRR. Another common mistake is failing to handle subscriptions that span the entire month but don't start or end within it. For the annual plan part, simply ignoring the plan_type or dividing all prices by 12 shows a lack of attention to detail. Not asking clarifying questions about ambiguous columns or date logic is also a missed opportunity to demonstrate seniority.
What usually comes next
Expect questions that build on this concept. For example: "How would you modify this to track MRR changes month-over-month, categorizing revenue as new, churn, expansion, or contraction?" or "This query runs on a 500 million row table and is slow. How would you optimize it?" (Answer: indexing, or preferably, creating a monthly summary table in a data model). Another likely question is, "How would you handle prorated amounts or discounts?"
A concrete example
To calculate MRR for October 2024, a good query would look like this. Note the use of 12.0 to prevent integer division. The logic correctly identifies all subscriptions that were active for any part of the month.
Select
SUM(CASE
WHEN plan_type = 'annual' THEN monthly_price / 12.0
ELSE monthly_price
END) AS mrr
FROM subscriptions
WHERE
start_date <= '2024-10-31'
AND (end_date > '2024-10-01' OR end_date IS NULL);Interview question
To accurately calculate the total Monthly Recurring Revenue (MRR) for a given month, which condition correctly identifies an 'active' subscription?
- a.The subscription's start_date is within the target month.
- b.The subscription's start_date is on or before the last day of the target month, AND its end_date is after the first day of the target month or is null.Correct
- c.The subscription's start_date is before the first day of the target month, and its end_date is after the last day of the target month.
- d.The subscription's start_date is before or on the last day of the target month, AND its end_date is before or on the last day of the target month.
Why? this is the answer
The card defines an active subscription as one that started on or before the last day of the month AND ends after the first day of the month (or has a null end date), which Option B accurately reflects. Option A incorrectly calculates only new MRR, a common mistake, while Options C and D are too restrictive and would miss many genuinely active subscriptions.
Just read this? Test yourself on what you have been reading.
Read the original → getdbt.com
- #sql
- #analytics
- #data modeling
- #business metrics
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on sql — each one lists the topics its interview covers.
See open roles