Skip to main content
Intermediatehow it works

How do you keep only the latest record per key?

Source: PostgreSQL: SELECT (clause processing)

Quick answerFree

ROW_NUMBER() OVER (PARTITION BY key ORDER BY updated_at DESC, tie_breaker DESC) in a CTE, then keep rn = 1. Mention the tie-breaker so the result is deterministic, and that in a warehouse you might use QUALIFY where supported.

Detailed answer· 40 words

Interview Prep

Unlock the full answer with Interview Prep

Get a 40-word model answer, 3 key points and a visual explainer, plus 247 more SQL questions.

Also in Pro (₹499 / 90 days).

Key points· 3 points

Interview Prep
Unlock with Interview Prep

Visual explainer· animated, step by step

Interview Prep
Unlock with Interview Prep

+ 247 more SQL questions in Interview Prep, each with the full answer.

Discussion