DataLane

14. Latest order per customer

mediumProNot started~12 minDeduplicationWindow functionsRanking

Asked in screens shaped like: Shopify, Square, Instacart

Build a one-row-per-customer table holding their most recent order. Two orders can share a timestamp, so the tie-break has to be deterministic or your pipeline produces different rows on every run.

Requirements

  • Columns: customer_id, order_id, ordered_at
  • Exactly one row per customer_id
  • Latest by ordered_at; ties broken by the lowest order_id
  • Order by customer_id

Expected output

customer_id, order_id, ordered_at

Row order is graded, so ORDER BY matters here.

SQL editor

Loading SQL engine…

Ctrl/Cmd + Enter submits

Run your query to see the result set.

Pro problem

Medium problems unlock with Pro

Easy pads stay free. Medium and hard SQL and Python problems — editor, tests, hints, and solutions — open after you upgrade to Pro or coaching.

See plansPractice free easy problems
↑↓ navigate openesc close