Why ROW_NUMBER() gives non-deterministic results with ties in ORDER BY, and how to make the ranking consistent

Quick Overview

Asks why ROW_NUMBER() can return different results across runs when rows tie on the ORDER BY columns, and how to make the ranking repeatable. Tests understanding of SQL ordering guarantees, top-row-per-group deduplication, and the choice between ROW_NUMBER, RANK and DENSE_RANK.

Why ROW_NUMBER() gives non-deterministic results with ties in ORDER BY, and how to make the ranking consistent

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the potential for non-deterministic results when using `ROW_NUMBER()` with ties in `ORDER BY`, and how to ensure consistent ranking. A common case is a query that keeps each customer's latest order with `ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC)` and then filters to row number 1. ```hint Look at the guarantee Ask what SQL actually promises about the relative order of two rows whose `ORDER BY` values are equal, and what inside the database could change that order from one run to the next. ``` ### What a Strong Answer Covers - Why `ROW_NUMBER()` has to break ties and why the engine's choice is not guaranteed to be stable. - What can change that choice between runs. - Where the problem causes real damage, such as deduplication and top-row-per-group queries. - How to make the ordering unique with a tie-breaker that also matches the business intent. - When `RANK()` or `DENSE_RANK()` fits better than `ROW_NUMBER()`. ### Follow-up Questions - The table has no primary key and some rows are exact duplicates. How do you deduplicate deterministically? - How do NULLs in the ordering column affect which row ranks first, and how do engines differ? - Does the same issue affect `LIMIT`/`OFFSET` pagination, and how does keyset pagination deal with it? - How would you write a check in a data pipeline that catches a non-unique ranking key before it causes trouble?

Overview: Asks why ROW_NUMBER() can return different results across runs when rows tie on the ORDER BY columns, and how to make the ranking repeatable. Tests understanding of SQL ordering guarantees, top-row-per-group deduplication, and the choice between ROW_NUMBER, RANK and DENSE_RANK.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the potential for non-deterministic results when using ROW_NUMBER() with ties in ORDER BY, and how to ensure consistent ranking.

A common case is a query that keeps each customer's latest order with ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) and then filters to row number 1.

What a Strong Answer Covers Guidance

  • Why ROW_NUMBER() has to break ties and why the engine's choice is not guaranteed to be stable.
  • What can change that choice between runs.
  • Where the problem causes real damage, such as deduplication and top-row-per-group queries.
  • How to make the ordering unique with a tie-breaker that also matches the business intent.
  • When RANK() or DENSE_RANK() fits better than ROW_NUMBER() .

Follow-up Questions Guidance

  • The table has no primary key and some rows are exact duplicates. How do you deduplicate deterministically?
  • How do NULLs in the ordering column affect which row ranks first, and how do engines differ?
  • Does the same issue affect LIMIT / OFFSET pagination, and how does keyset pagination deal with it?
  • How would you write a check in a data pipeline that catches a non-unique ranking key before it causes trouble?