PySpark Window Functions
19 interview problems on rank, lag, lead and running totals over partitions, from easy to hard.
Learn Window Functions
- row_number / rank · 4 min read. Number and rank rows inside a window, and how the three ranking functions treat ties.
- lag / lead · 4 min read. Read the previous or next row in a window to compute changes, gaps and streaks.
- rangeBetween · 6 min read. Window frames by value instead of row count: the last 7 calendar days, not the last 7 rows.
- Gaps and islands · 7 min read. Streaks, sessions and status runs: one window pattern behind many interview questions.
- last(ignorenulls) · 4 min read. Forward-fill missing values with last over a window, and the frame that makes it work.
- Latest Order per Customer · Medium · Window Functions, Filtering & Selection
- Running Total by Region · Medium · Window Functions
- Top Two Salary Levels per Department · Medium · Window Functions, Filtering & Selection
- Month-over-Month Change · Medium · Window Functions
- 7-Day Moving Average · Medium · Window Functions
- Share of Category Total · Medium · Window Functions
- Customer Spend Quartiles (NTILE) · Medium · Window Functions
- Percent Rank and Cumulative Distribution · Medium · Window Functions
- Remove Near-Duplicate Documents · Medium · Data Prep for AI, Window Functions, Strings
- Three-Day Login Streak · Hard · Window Functions, Aggregations, Dates, Filtering & Selection
- Trailing 3 Calendar Days (Range Frame) · Hard · Window Functions, Dates, Filtering & Selection
- Forward-Fill Missing Readings · Hard · Window Functions
- Sessionize a Clickstream · Hard · Window Functions, Dates, Null Handling, Conditional Logic, Filtering & Selection
- Consecutive Status Runs (Gaps and Islands) · Hard · Window Functions, Aggregations
- Build an SCD Type 2 History · Hard · Window Functions, Null Handling, Filtering & Selection
- Upsert: Merge New Records into a Table · Hard · Window Functions, Filtering & Selection
- Apply a CDC Batch (MERGE Semantics) · Hard · Lakehouse, Window Functions, Joins
- Fit Conversations into a Context Window · Hard · Data Prep for AI, Window Functions, Joins
- Build Preference Pairs from Ratings · Hard · Data Prep for AI, Window Functions, Joins
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice