Interactive Lab · Data & SQL

GROUP BY throws your rows away, a window function keeps every one

A GROUP BY collapses many rows into one summary line, the detail is gone. A window function computes across a set of rows (the "window") but adds the answer as a new column and keeps every original row. That's the superpower: running totals, rankings, and row-to-row comparisons without losing a single row. Pick a function below and watch the new column appear.

🎬 Watch the 90-second explainer, then try each window function below ↓

▶ The playground, pick a function, flip the clauses

the computed window column (kept per row) partition group (independent window)
↓

The result, every original row survives, plus one new column

regionmonthamount row_num
rows in 0 rows out 0 windows 0 vs GROUP BY → 0 rows

Window vs GROUP BY, side by side

Ask "total sales per region." GROUP BY region answers with 2 rows, one number per region, and every individual sale is gone. SUM() OVER (PARTITION BY region) answers the same question but stamps that total onto all 9 detail rows, you keep the month, the amount, and the total, side by side. Same aggregate, zero rows lost. That's why window functions are how analysts do running totals and rankings without a self-join.

The SQL that's actually running



    

🧭 Where this fits

Window functions are the modern analyst's power tool: running revenue totals, per-region rankings, top-N-per-group, retention cohorts, all without collapsing rows like GROUP BY. The RANK-vs-ROW_NUMBER tie behavior you just predicted is a top-3 SQL interview question and a real bug source in leaderboards and dedup logic.

🔮 Predict, then test

Turn PARTITION BY region ON and pick SUM() running, watch the running total reset to zero at each new region (each partition is its own private window). Now switch from ROW_NUMBER to RANK on the amount column, which has ties. Before you look: where do the numbers differ, and why does RANK skip the value right after a tie? (Hint: two rows tied for 3rd means the next row is 5th, not 4th, ROW_NUMBER never does that.)

The takeaway

Your new mental model of window functions