Skip to main content
SQL โ€ข LESSON 171

Duplicate Removal Pattern

How can we deduplicate records by keeping only the most recently created entry?

Advancedโฑ 3 Minutesโญ 1840 XP
๐Ÿค” THE QUESTION

How can we deduplicate records by keeping only the most recently created entry?

๐Ÿ’ก WHAT IS IT?

Ranking partitions by created_at DESC with ROW_NUMBER() and filtering for rn = 1 removes older duplicates.

๐ŸŽฏ WHAT IS IT USED FOR?

Golden customer records, warehouse deduplication staging, event stream cleanup.

๐Ÿ’ป EXAMPLE
SELECT *
FROM (
    SELECT
        *,
        ROW_NUMBER() OVER (
            PARTITION BY email
            ORDER BY created_at DESC
        ) AS rn
    FROM customers
) deduplicated
WHERE rn = 1;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for Duplicate Removal Pattern.

  • โœ“ Deduplication pattern
  • โœ“ Latest record retention
  • โœ“ rn = 1 filter
  • โœ“ Golden record creation