Truncating Dates into Periods First

Concepts covered: sqlSelfJoin

The pre-aggregation is mandatory. Computing period-over-period over raw transactions without first rolling up to the period grain produces a fan-out: every transaction in the current month pairs with every transaction in the prior month. The numbers inflate, the dashboard shows nonsense, and the candidate spends the rest of the interview explaining what went wrong. The right shape is always two CTEs: one to compute the per-period totals, one to compare each period to its predecessor. The query you should be able to write from memory Why each CTE matters The monthly CTE pre-aggregates transactions to the (region, month) grain. The compared CTE pairs each month with its predecessor using LAG. The final SELECT computes the delta and the growth rate. Three steps, each with a single job. Readin

About This Interactive Section

This section is part of the Period-over-Period: Beginner lesson on DataDriven, a free data engineering interview prep platform. Each section includes explanations, worked examples, and hands-on code challenges that execute in real time. SQL queries run against a live database. Python runs in a sandboxed Docker container. Data modeling problems validate against interactive schema canvases. All content is framed around what data engineering interviewers actually test at companies like Meta, Google, Amazon, Netflix, Stripe, and Databricks.

How DataDriven Lessons Work

DataDriven combines four interview rounds (SQL, Python, Data Modeling, Pipeline Architecture) with adaptive difficulty and spaced repetition. Easy problems get harder as you improve. Weak concepts resurface until you master them. Your readiness score tracks progress across every topic interviewers test. Every lesson section ends with problems you solve by writing and running real code, not by picking multiple-choice answers.