Intermediate Case 29 of 50

Case 029: The Second Order

orders

"First orders are easy to find,"

the manager says. "Could you find the SECOND order for each customer? That's the one that tells us if they'll actually come back."

The data Alex is looking at

orders
idcustomer_idorder_date
10112026-01-05
10222026-01-08
10332026-01-12
10412026-01-20
10542026-02-01
… 23 more rows
TASK

Help Alex answer the manager:

“Find each customer's second order (chronologically).”
SQL editor Ctrl+Enter to run
Write a query above and hit Run to see what comes back.
No hints requested yet — click "Get a hint" when you're stuck.
Alex's final query — this is for reference. Copy it into your own thinking, not into the editor above.
WITH ranked_orders AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn FROM orders ) SELECT id, customer_id, order_date FROM ranked_orders WHERE rn = 2;
What that query returns
Case Debrief CTE + ROW_NUMBER
  • CTEs and window functions combine constantly — name the ranked version of your data, then filter on the rank in a plain WHERE.

Case closed.

Ready for the next one?

Next: Case 030 — Signup Cohorts →