D8LooPFocus modeCODE WORKSPACE
Return the latest status per order using ROW_NUMBER over order_id ordered by updated_at DESC.
Start by naming the output grain, then build CTEs that can be inspected one at a time.
Sample input
| order_id | status | updated_at |
|---|---|---|
| 1001 | paid | 2026-01-12 09:00:00 |
| 1001 | shipped | 2026-01-13 10:00:00 |
| 1004 | paid | 2026-01-15 14:30:00 |
| 1004 | shipped | 2026-01-16 12:20:00 |
Expected output
| order_id | status | updated_at |
|---|---|---|
| 1001 | shipped | 2026-01-13 10:00:00 |
| 1002 | paid | 2026-01-12 10:30:00 |
| 1004 | shipped | 2026-01-16 12:20:00 |
Constraints
State the output contract first, use readable CTE layers, return the exact columns requested by the prompt, and use deterministic ordering.
Expected skills
Interview clarification, SQL structure, edge-case handling, and final-answer narration.
Run the SQL query to inspect preview rows.