Medium ยท Interview Practice

SQL Window Functions Medium Challenge

A real SQL interview round, built around ROW_NUMBER, RANK, DENSE_RANK, PARTITION BY, Running Totals, LAG and LEAD.

โฑ30 Minutes
๐Ÿ“15 Challenges
๐Ÿ’ฏ100 Points
๐Ÿ†Certificate Eligible
Challenges 1โ€“55 pts each
Challenges 6โ€“107 pts each
Challenges 11โ€“158 pts each
Total100 points ยท Pass at 70
Before you begin
  • You have exactly 30 minutes. The timer starts when you click "Start Challenge".
  • The sandbox uses a real e-commerce schema: categories, users, products, orders, order_items, payments.
  • Every challenge requires a specific window function โ€” result matching alone is not enough, your query must actually use it.
  • You can run queries to preview results. Answers are only graded on final submission.
  • Passing unlocks a shareable certificate and a spot on the Window Functions Medium Challenge leaderboard.

You need to be logged in to take this challenge.

Learn

What Are SQL Window Functions?

Window functions compute a value across a set of rows related to the current row โ€” a "window" โ€” without collapsing those rows into a single output row the way GROUP BY does. Once you're comfortable with GROUP BY, HAVING, JOINs, and basic aggregates, window functions are the natural next step toward interview-ready SQL.

This challenge is built around a realistic e-commerce dataset โ€” customers, orders, order items, products, categories, and payments โ€” because that is the shape of data window-function questions are asked against at Amazon, Flipkart, Swiggy, Zomato, Razorpay, Deloitte, Accenture, TCS Digital, Infosys, and analytics consulting firms.

Why Data Analysts Need Window Functions

Nearly every analytics, BI, and data-engineering role eventually needs a "top customer per category," a "month-over-month growth," or a "running total" query โ€” and window functions are the idiomatic, performant way to write them. Interviewers use these questions to separate candidates who memorized SELECT/JOIN/GROUP BY from candidates who can reason about partitions, frames, and ordering the way a production analytics pipeline requires.

ROW_NUMBER vs RANK vs DENSE_RANK

All three assign a position to each row within an ordered partition, but they disagree on ties. ROW_NUMBER() always hands out a unique, gapless number even when values tie. RANK() gives tied rows the same number but skips the next position (1, 2, 2, 4). DENSE_RANK() also gives tied rows the same number but never skips a position (1, 2, 2, 3). Picking the wrong one is one of the most common SQL interview mistakes.

Understanding PARTITION BY

PARTITION BY resets a window function at the start of each group โ€” "rank products within each category," or "compare each order to the customer's previous order" โ€” instead of ranking or comparing across the entire result set. It is to window functions what GROUP BY is to aggregates, except the underlying rows are never collapsed.

Common Window Functions Covered in This Challenge

ROW_NUMBER

Assigns a unique, gapless sequence number to each row within a partition โ€” the foundation of "top-N per group" queries.

RANK

Ranks rows within a partition, leaving gaps after ties (1, 2, 2, 4).

DENSE_RANK

Ranks rows within a partition without gaps after ties (1, 2, 2, 3).

PARTITION BY

Splits a result set into independent groups so a window function resets and re-runs within each group.

SUM() OVER()

Computes a running or cumulative total across ordered rows โ€” the backbone of revenue and growth trend lines.

LAG

Looks back to a previous row in the same partition โ€” the backbone of period-over-period comparisons.

LEAD

Looks ahead to a following row in the same partition โ€” used for "what happens next" analysis.

NTILE

Splits ordered rows into N roughly equal buckets โ€” used for percentile/quartile segmentation.

SQL Window Functions Interview Questions

Data analyst, BI analyst, analytics engineer, and product analyst interviews consistently include at least one window-function question โ€” most often ranking within a group (ROW_NUMBER/RANK/DENSE_RANK) or a period-over-period comparison (LAG/LEAD). This challenge mirrors that format: 15 scenario-driven questions, each requiring a specific window function, evaluated against real query output โ€” not multiple choice.

Passing at 70/100 unlocks a shareable certificate with your score and completion date, and every attempt is eligible for the Window Functions Medium Challenge leaderboard. Once you're consistently passing here, the Window Functions Hard Challenge is the natural next step.

Related Challenges