Skip to main content
SQL โ€ข LESSON 187

Latest Customer Record

How can we retrieve the latest version of every customer record?

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

How can we retrieve the latest version of every customer record?

๐Ÿ’ก WHAT IS IT?

Partitioned window ranking on updated_at extracts current SCD Type 1 customer snapshots.

๐ŸŽฏ WHAT IS IT USED FOR?

Dimension snapshot resolution, customer master data, deduplicating update histories.

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

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for Latest Customer Record.

  • โœ“ Latest snapshot resolution
  • โœ“ SCD Type 1 staging
  • โœ“ ROW_NUMBER partition
  • โœ“ Master data management