Skip to main content
SQL โ€ข LESSON 172

Latest Record

How can we retrieve the latest state update for every customer?

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

How can we retrieve the latest state update for every customer?

๐Ÿ’ก WHAT IS IT?

Partitioning by customer_id and ordering by updated_at DESC isolates the latest entity revision.

๐ŸŽฏ WHAT IS IT USED FOR?

Change Data Capture (CDC) state resolution, current profile lookup, SCD Type 1 staging.

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

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for Latest Record.

  • โœ“ CDC state resolution
  • โœ“ Latest update extraction
  • โœ“ ROW_NUMBER partition
  • โœ“ State snapshotting