How can window functions, specifically ROW_NUMBER(), be used for deduplication in SQL?

Quick Overview

Asks how to deduplicate rows in SQL with the ROW_NUMBER() window function, both when selecting one row per duplicate group and when deleting the extra copies from a table. Tests window function semantics, deterministic tie-breaking, NULL handling, and engine differences when deleting duplicates.

How can window functions, specifically ROW_NUMBER(), be used for deduplication in SQL?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How can window functions, specifically `ROW_NUMBER()`, be used for deduplication in SQL? Explain the query pattern for selecting one row per duplicate group, and how you would remove the extra copies from the table itself. ```hint Three decisions Decide which columns make two rows "the same", which copy you want to keep, and what happens when two copies tie on that choice. ``` ### What a Strong Answer Covers - The window-function query pattern for keeping one row per duplicate group, and what each clause decides. - Why `ROW_NUMBER()` is the right function here and what `RANK()` or `DENSE_RANK()` would do instead. - A deterministic tie-breaker, and how NULLs in the partition and ordering columns behave. - How to delete the duplicates from the table, and how that differs across database engines. - Shorter alternatives where an engine supports them. ### Follow-up Questions - How would you remove duplicates in PostgreSQL from a table that has no primary key? - Two copies of a customer record each hold non-null values the other lacks. How would you merge them into one row instead of picking one? - How would you deduplicate an append-only event table with billions of rows that receives new data every hour? - When would you keep the earliest copy instead of the latest?

Overview: Asks how to deduplicate rows in SQL with the ROW_NUMBER() window function, both when selecting one row per duplicate group and when deleting the extra copies from a table. Tests window function semantics, deterministic tie-breaking, NULL handling, and engine differences when deleting duplicates.

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

How can window functions, specifically ROW_NUMBER(), be used for deduplication in SQL?

Explain the query pattern for selecting one row per duplicate group, and how you would remove the extra copies from the table itself.

What a Strong Answer Covers Guidance

  • The window-function query pattern for keeping one row per duplicate group, and what each clause decides.
  • Why ROW_NUMBER() is the right function here and what RANK() or DENSE_RANK() would do instead.
  • A deterministic tie-breaker, and how NULLs in the partition and ordering columns behave.
  • How to delete the duplicates from the table, and how that differs across database engines.
  • Shorter alternatives where an engine supports them.

Follow-up Questions Guidance

  • How would you remove duplicates in PostgreSQL from a table that has no primary key?
  • Two copies of a customer record each hold non-null values the other lacks. How would you merge them into one row instead of picking one?
  • How would you deduplicate an append-only event table with billions of rows that receives new data every hour?
  • When would you keep the earliest copy instead of the latest?