Window Functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE)
Rank and bucket rows over partitions without collapsing the grain of the result set.
In plain words
Define peer groups. Next, define deterministic ranking direction. Finally, consume the result in an outer query.
Think of it like…
A window is a scoreboard beside a queue: people remain in line while their rank, percentile, or running score is written on a card.
Where you’ve seen it
ROW_NUMBER() OVER (PARTITION BY tenant_id ORDER BY updated_at DESC, event_id DESC) AS rn yields the single newest row per tenant deterministically; filter rn=1 in the outer query.
Words you’ll hear
- ROW_NUMBER
- A unique sequential number per partition based on the specified ordering, with ties broken by later order keys or arbitrarily.
- RANK
- A rank with gaps after ties, such as 1,1,3;
DENSE_RANKinstead produces 1,1,2. - NTILE
- A requested number of near-equal buckets over an ordered partition, useful for quantiles but not guaranteed to make equal peer groups.
- Operational boundary
RANKleaves gaps after ties,DENSE_RANKdoes not, andROW_NUMBERarbitrarily breaks ties unless ORDER BY includes deterministic tie-breakers. Declare the business partition and deterministic order, select the ranking function that matches the tie rule, and add a final filter on a subquery because window values are not available in the same WHERE.
Visual diagram
Theory & deep dive
ROW_NUMBER: the working mechanism
Window functions calculate a value for each row after grouping rows into partitions, ordering them, and optionally applying a frame-without collapsing those rows into one aggregate output.
Define peer groups. Define deterministic ranking direction, then rOW_NUMBER, RANK, DENSE_RANK, or NTILE; this exposes the state governed by NTILE.
ROW_NUMBER() OVER (PARTITION BY tenant_id ORDER BY updated_at DESC, event_id DESC) AS rn yields the single newest row per tenant deterministically; filter rn=1 in the outer query.
Window Functions: failure mode and production response
RANK leaves gaps after ties, DENSE_RANK does not, and ROW_NUMBER arbitrarily breaks ties unless ORDER BY includes deterministic tie-breakers.
Declare the business partition and deterministic order, select the ranking function that matches the tie rule, and add a final filter on a subquery because window values are not available in the same WHERE. Track Tie policy as "RANK includes gaps, DENSE_RANK does not", and make Tie-breaker visible to the dataset owner.
How it works, step by step
Step 1 of 41. Partition
Define peer groups. Window functions calculate a value for each row after grouping rows into partitions, ordering them, and optionally applying a frame-without collapsing those rows into one aggregate output.
- Tie policy
- RANK includes gaps, DENSE_RANK does not
- RANK
- A rank with gaps after ties, such as 1,1,3; `DENSE_RANK` instead produces 1,1,2.
When to use it & trade-offs
What it is
Window functions calculate a value for each row after grouping rows into partitions, ordering them, and optionally applying a frame-without collapsing those rows into one aggregate output. ROW_NUMBER() OVER (PARTITION BY tenant_id ORDER BY updated_at DESC, event_id DESC) AS rn yields the single newest row per tenant deterministically; filter rn=1 in the outer query.
Why it matters: RANK leaves gaps after ties, DENSE_RANK does not, and ROW_NUMBER arbitrarily breaks ties unless ORDER BY includes deterministic tie-breakers. Declare the business partition and deterministic order, select the ranking function that matches the tie rule, and add a final filter on a subquery because window values are not available in the same WHERE.
When to reach for it
- Rank and bucket rows over partitions without collapsing the grain of the result set.
- Choose ROW_NUMBER because it provides this capability: A unique sequential number per partition based on the specified ordering, with ties broken by later order keys or arbitrarily.
- Keep evidence for Tie policy at "RANK includes gaps, DENSE_RANK does not" so the last production stage-Filter - consume the result in an outer query-remains testable.
When to avoid or be careful
RANKleaves gaps after ties,DENSE_RANKdoes not, andROW_NUMBERarbitrarily breaks ties unless ORDER BY includes deterministic tie-breakers.- Do not conflate RANK with ROW_NUMBER. The second concept contributes different behavior: A rank with gaps after ties, such as 1,1,3;
DENSE_RANKinstead produces 1,1,2. - Release only after this guardrail is in place: Declare the business partition and deterministic order, select the ranking function that matches the tie rule, and add a final filter on a subquery because window values are not available in the same WHERE.
ROW_NUMBER / RANK / NTILE - roles, limits, and operating cost
Strengths: A unique sequential number per partition based on the specified ordering, with ties broken by later order keys or arbitrarily. Window functions calculate a value for each row after grouping rows into partitions, ordering them, and optionally applying a frame-without collapsing those rows into one aggregate output.
Watch out: RANK leaves gaps after ties, DENSE_RANK does not, and ROW_NUMBER arbitrarily breaks ties unless ORDER BY includes deterministic tie-breakers.
DENSE_RANK instead produces 1,1,2.Strengths: A rank with gaps after ties, such as 1,1,3; DENSE_RANK instead produces 1,1,2. In this workflow it supports: Rank - ROW_NUMBER, RANK, DENSE_RANK, or NTILE.
Watch out: The operating cost is real: include a stable key after timestamp.
Strengths: A requested number of near-equal buckets over an ordered partition, useful for quantiles but not guaranteed to make equal peer groups. Its output can be checked with NTILE size.
Watch out: Operating guardrail: Declare the business partition and deterministic order, select the ranking function that matches the tie rule, and add a final filter on a subquery because window values are not available in the same WHERE.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for ROW_NUMBER, and how would Tie policy reveal it?”
- “Why is RANK distinct from this behavior: a requested number of near-equal buckets over an ordered partition, useful for quantiles but not guaranteed to make equal peer groups?”
- Production clue: RANK leaves gaps after ties, DENSE_RANK does not, and ROW_NUMBER arbitrarily breaks ties unless ORDER BY includes deterministic tie-breakers.
- Design probe: Declare the business partition and deterministic order, select the ranking function that matches the tie rule, and add a final filter on a subquery because window values are not available in the same WHERE.
Benchmarks & rules of thumb
- Tie policy
- RANK includes gaps, DENSE_RANK does not
- Tie-breaker
- include a stable key after timestamp
- NTILE size
- early buckets can receive one extra row
Common mistakes & follow-ups
Common interview mistakes
RANKleaves gaps after ties,DENSE_RANKdoes not, andROW_NUMBERarbitrarily breaks ties unless ORDER BY includes deterministic tie-breakers.- Reporting success from a job exit code without checking Tie policy and Tie-breaker.
- Treating this example as complete without replay, rollback, or access boundaries:
ROW_NUMBER() OVER (PARTITION BY tenant_id ORDER BY updated_at DESC, event_id DESC) AS rnyields the single newest row per tenant deterministically; filterrn=1in the outer query.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: A unique sequential number per partition based on the specified ordering, with ties broken by later order keys or arbitrarily.
- What does the recovery plan do when this occurs?
RANKleaves gaps after ties,DENSE_RANKdoes not, andROW_NUMBERarbitrarily breaks ties unless ORDER BY includes deterministic tie-breakers. - When is ROW_NUMBER insufficient for Tie-breaker, and what does adding RANK cost in Tie policy?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Window Functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE) with data engineer interview Q&A and hands-on practice.