Skip to main content
SQL • LESSON 172

Latest Record

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

Advanced3 Minutes1850 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