This one took me longer to mentally parse than I expected.
Start by clarifying the schema and definitions, then build the query in logical steps: identify new paid subscribers, aggregate monthly counts, generate a complete month series, and finally join to the prior year to compute YoY metrics. Use CTEs for readability and ensure edge cases like zero months and missing prior-year data are handled with LEFT JOINs and COALESCE.
Pro tip: Mention that you would validate the exclusion logic by checking for companies that ever had a free subscription, and consider using a NOT EXISTS clause for performance. Also, explicitly state how you handle the latest month and ensure the date range is dynamic.
Confirm the table structure, definitions of 'new paid subscriber', 'free subscription', and how to identify the latest month. Ask about timezone and date truncation.
Write a subquery to select companies with non-null subscriptiondate in the target months, excluding those that ever had a free subscription (e.g., using NOT EXISTS or LEFT JOIN with NULL check).
Group the filtered companies by month (date_trunc('month', subscriptiondate)) and count distinct companies to get monthly new paid subscriber counts.
Use generate_series to create a continuous sequence of months from June 2019 to the latest month in the data, then LEFT JOIN the aggregated counts to include zero months.
Self-join the monthly counts to the same month in the prior year (using date subtraction or interval), then calculate absolute change (current - prior) and percent change ((current - prior)/prior * 100), handling NULLs with COALESCE.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.