Window Functions
Asked of: Data Scientist
Last updated

What's being tested
These problems test window-function analytics over customer, order, driver, restaurant, and city-level event data. You need to partition entities, order events in time, compare current rows to prior rows, rank within groups, and combine row-level windows with aggregations without losing analytical meaning.
Patterns & templates
-
Prior-event comparison — use
LAG(value) OVER (PARTITION BY customer_id ORDER BY order_ts)to compute spend changes, reorder gaps, or previous restaurant/city. -
Next-event comparison — use
LEAD(status) OVER (...)when analyzing driver request sequences, acceptance funnels, or what happened after a dispatch attempt. -
Ranking within groups — use
ROW_NUMBER,RANK, orDENSE_RANKwithPARTITION BY city_id/restaurant_id; choose based on tie behavior. -
Top-N per segment — wrap window output in a CTE, then filter
WHERE rn <= N; avoid filtering window aliases in the sameSELECT. -
Running totals and rolling metrics — use
SUM(amount) OVER (PARTITION BY user_id ORDER BY order_ts ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)for cumulative spend. -
Percentile segmentation — use
NTILE(4),PERCENT_RANK, orCUME_DISTfor quartiles; validate whether equal-sized buckets or true percentile thresholds are intended. -
Aggregate then window — first group to
restaurant/day/customerlevel, then apply windows; mixing raworderrows with grouped metrics often duplicates revenue.
Common pitfalls
Pitfall: Using
RANKwhen the interviewer expects exactly one row per group; useROW_NUMBERwith a deterministic tie-breaker likeorder_id.
Pitfall: Forgetting that window functions run after
WHERE, so filtering dates or statuses too early can remove rows needed for prior-event comparisons.
Pitfall: Ordering only by date when multiple orders occur on the same day; include timestamp and stable IDs to make results reproducible.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Calculate Order Request MetricsDoorDash · Data Scientist · Onsite · hard
- Analyze Restaurant Customer MetricsDoorDash · Data Scientist · Technical Screen · medium
- Write SQL for monthly spend and ratiosDoorDash · Data Scientist · Technical Screen · medium
- Compute rolling cold-delivery rates with windowsDoorDash · Data Scientist · Onsite · Medium
- Write SQL for percent and window changesDoorDash · Data Scientist · Technical Screen · Medium
- Write SQL for cold-complaint diagnostics with LAG/QUALIFYDoorDash · Data Scientist · Technical Screen · Medium
- Write SQL to backtest refund policyDoorDash · Data Scientist · Technical Screen · Medium
- Write complex SQL on DoorDash dataDoorDash · Data Scientist · Onsite · Medium
- Solve multi-part SQL with sliding windowsDoorDash · Data Scientist · Technical Screen · Medium
- Write SQL for cuisine median delivery timesDoorDash · Data Scientist · Onsite · Medium
- Analyze Order Spending Patterns Across Cities Using SQLDoorDash · Data Scientist · Technical Screen · Medium
- Analyze Driver Requests for Food Delivery OrdersDoorDash · Data Scientist · Technical Screen · Medium
Related concepts
- SQL Window Functions And AnalyticsData Manipulation (SQL/Python)
- Window Functions, Cohorting, and Time Series SQL
- SQLData Manipulation (SQL/Python)
- SQL Window Functions And Temporal JoinsData Manipulation (SQL/Python)
- SQL Window Functions And Analytical QueryingData Manipulation (SQL/Python)
- Cohorts And Window Functions In SQL