SQL SQL In Depth
0%
Guided SQL & Engine Coding Lab

Clickstream Sessionizer, Streak Tracker & Rolling Window Analyzer

An analytical window function engine implementing LAG()-based clickstream sessionization, ROW_NUMBER() date-difference islands for consecutive activity streaks, and ROWS vs RANGE sliding window frames.

Guided SQL & Storage Engine Lab 0/3 Steps Completed

Execute & Verify SQL & Storage Engine Invariants in the Browser SQL Studio

Complete 3 progressive relational steps with guided hints in PostgreSQL 18 WASM, SQLite 3 & Step-by-Step Relational Table Visualizer against automated result-set and query-plan assertions — zero local setup required.

01 Phase 1 · Base Relation & SARGable Predicate
Base Relation Scan & SARGable Filter (fintech_ledger)
Establish the base relation scan in fintech_ledger for Hands-On Build: Clickstream Sessionizer, Streak Tracker & Rolling Window Analyzer with a SARGable predicate and explicit Three-Valued Logic NULL handling.
02 Phase 2 · Relational Join, Aggregation & 3VL Guard
Relational Join, Grouping & NULL-Safe Aggregation
Combine normalized tables via explicit JOIN conditions and fold group buckets or window partitions without losing NULL boundary rows.
03 Phase 3 · Production Query & Plan Verification
Production Query & Execution Plan Verification (Hands-On Build: Clickstream Sessionizer, Streak Tracker & Rolling Window Analyzer)
Verify final column projections, ordering, and physical B+Tree access paths via EXPLAIN QUERY PLAN and automated relational assertions.

Architectural Overview

An analytical window function engine implementing LAG()-based clickstream sessionization, ROW_NUMBER() date-difference islands for consecutive activity streaks, and ROWS vs RANGE sliding window frames.

In production relational and analytical database systems—spanning PostgreSQL, SQLite, MySQL/InnoDB, and DuckDB—understanding how query clauses, schema constraints, index structures, and storage engines execute under the hood separates surface-level query authors from staff-level database engineers. This capstone build lab guides you through implementing Clickstream Sessionizer, Streak Tracker & Rolling Window Analyzer from first principles with deterministic self-test invariants.

Guided Implementation Walkthrough (4 Progressive Steps)

Step 1: Clickstream Sessionization via LAG() & Cumulative Sum

  • Core Database Concept: Two-Stage Window Pipeline: Inactivity Flagging + Running Session Counter
  • Target Module: index.js (WindowAnalyticsEngine.sessionizeEvents)
  • Engineering Specification: Implement sessionizeEvents(events, timeoutSec) to partition events by user_id, order by tsSec, compute LAG(tsSec), flag new sessions whenever tsSec - prevTs > timeoutSec, and assign a running session_id via cumulative sum.
  • Implementation Hint: Sort by user_id, tsSec; mark isNewSession = (prevTs === null || tsSec - prevTs > timeoutSec ? 1 : 0), then accumulate session_num.
  • Verification Invariant: deterministic assertion verified in test.js step 1

Step 2: Gaps-and-Islands Consecutive Day Streak Finder

  • Core Database Concept: Constant Difference Invariant: dayIndex - ROW_NUMBER() OVER (PARTITION BY user ORDER BY dayIndex)
  • Target Module: index.js (WindowAnalyticsEngine.computeStreaks)
  • Engineering Specification: Implement computeStreaks(activityDays) using the classic Gaps-and-Islands invariant: subtracting sequential ROW_NUMBER() from integer dayIndex yields a constant islandKey across consecutive days.
  • Implementation Hint: Deduplicate (user_id, dayIndex), sort ascending, group by islandKey = dayIndex - rowNumber, and return streak start, end, and length.
  • Verification Invariant: deterministic assertion verified in test.js step 2

Step 3: Physical ROWS Window Frame Moving Average

  • Core Database Concept: Evaluating ROWS BETWEEN k PRECEDING AND CURRENT ROW
  • Target Module: index.js (WindowAnalyticsEngine.movingAvgRows)
  • Engineering Specification: Implement movingAvgRows(rows, metricCol, precedingRows) to compute the physical row-count sliding window average.
  • Implementation Hint: For index i, slice rows from Math.max(0, i - precedingRows) to i + 1 and average the metric.
  • Verification Invariant: deterministic assertion verified in test.js step 3

Step 4: Logical RANGE Window Frame Sum with Peer Ties

  • Core Database Concept: Evaluating RANGE BETWEEN delta PRECEDING AND CURRENT ROW Including All Peer Ties
  • Target Module: index.js (WindowAnalyticsEngine.movingSumRange)
  • Engineering Specification: Implement movingSumRange(rows, orderCol, metricCol, delta) to sum all tuples whose orderCol falls within [curOrder - delta, curOrder], including all duplicate peer timestamps.
  • Implementation Hint: Include all rows j where rows[j].orderVal >= rows[i].orderVal - delta && rows[j].orderVal <= rows[i].orderVal.
  • Verification Invariant: deterministic assertion verified in test.js step 4

Relational & Storage Engine Guarantees

  • Curriculum Domain: Window Functions & Analytics
  • Difficulty & Time Budget: Advanced (~40 minutes)
  • Key Architectural Highlights:
    • Clickstream sessionization via LAG(ts) inactivity threshold detection and cumulative SUM() session IDs
    • Gaps-and-Islands streak length calculation using the date - ROW_NUMBER() constant-difference invariant
    • Exact physical ROWS vs logical value-based RANGE window frame evaluation with peer grouping
  • Core Primitives & Tags: Window Functions, Sessionization, Gaps and Islands, ROWS vs RANGE, Sliding Windows

Related Curriculum Units

Production Database Context & Verification

This from-scratch relational engine component is engineered with zero external dependencies and deterministic verification harnesses. Every internal database invariant—from three-valued logic (TRUE / FALSE / UNKNOWN), referential integrity, and fan-out-free join pre-aggregation to B+Tree page splits, MVCC snapshot visibility (xmin / xmax), slotted-page compaction, and ARIES write-ahead log crash recovery—is asserted directly in test.js. Use the interactive browser workbench above to complete the 4 guided steps in Guided Lab mode or inspect and benchmark the complete reference architecture in Full Solution mode.