Comparing This Period to the Last
Concepts covered: sqlLagLead
Once you have written the LAG-based query, the interviewer will sometimes ask 'now do it without LAG.' The alternative is a self-join on offset dates. Both produce the same answer; both are common in real codebases; knowing both is the floor at this level. Knowing which one to reach for first is the move. The self-join alternative The self-join produces the same output as the LAG-based query. The differences are stylistic and semantic. The self-join references the monthly CTE twice; the reader's eyes track two copies of the same data. The LAG version references it once; the partition makes the per-region boundary explicit. For the immediately-previous-month case, LAG reads cleaner; both perform similarly on modern engines. 12 When the self-join wins If the comparison is not immediately-pre
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.