Interview Prep

5 Window Traps That Lie

Basket-split LAG gaps, ghost orders, nondeterministic ROW_NUMBER, RANK gaps, unframed running totals: 5 window traps with fixes and SQL included.

Anuj SainiOct 6, 20262 min read

1. The basket-split LAG trap

MIN(order_date - LAG(order_date)) per customer. In quick commerce ~8% of orders land within 30 minutes — forgotten coriander, not replenishment. Fix: exclude same-day gaps and use the median distribution, not MIN.

2. Ghost orders

Cancelled order re-placed 5 minutes later counts a logistics failure as loyalty. Fix: filter status = 'delivered' before windowing.

3. Nondeterministic ROW_NUMBER

Ties broken arbitrarily. Fix: always add a second ORDER BY key (e.g. order_id).

4. The RANK gap

Ranks 1,2,2,4 with exactly-3-rows required. Fix: switch to DENSE_RANK and disclose tie inflation.

5. The unframed running total

Defaults differ by dialect. Fix: state the frame explicitly — ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

Writing SQL is the easy 20%. Scoping what the metric means is the 80% that gets you hired. Drill windows on messy data at Topfolio Practice.

Related: Sql Rank Dense Rank Tie Traps · Sql Date Patterns Interview

Frequently Asked Questions

Why is MIN with LAG dangerous for repeat-purchase gaps?

Raw MIN captures the most extreme outlier — same-day basket splits and cancelled re-orders. Use the median distribution excluding same-day top-ups.

Do I need to filter before applying window functions?

Yes. Ghost orders (cancelled then re-ordered) and test rows must be filtered first, or the window faithfully computes over garbage.

What is a ghost order?

A cancelled order re-placed minutes later. Without a status filter, window math counts a logistics failure as customer loyalty.

Anuj Saini

Written by

Anuj SainiFounder & Lead Instructor

Founder at Topfolio with 6+ years in data & analytics across JPMC, Ultrahuman, and high-growth startups. Sat on hiring panels, reviewed 500+ resumes, and writes practical SQL & data guides.