The ROW_NUMBER Difference Trick

Concepts covered: sqlLagLead

Here is the question that recurs in interviews past the basics. 'Compute user sessions from an events table, where a session is a sequence of events with no gap longer than thirty minutes.' This is gaps and islands with a threshold instead of strict adjacency. The row-number difference trick from the beginner lesson does not apply directly; the gap is not 'value plus one,' it is 'value within some delta.' The LAG-plus-cumulative-SUM form generalizes to this case. Knowing when to reach for it, and being able to explain why both forms produce the same answer, is the depth this question rewards. The two canonical forms What the interviewer is silently scoring Three things, in order. First: do you recognize the variant on first read? Sessionization with a threshold is not the same shape as 'lo

About This Interactive Section

This section is part of the Gaps and Islands: Intermediate 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.