Back to Practice
#0206

Second Purchase Date

MediumSQL25 min20 XP

Problem

Return each user_id and the date of their second purchase. Users with fewer than two purchases should not appear.

Why This Matters

Retention, repeat purchase, and lifecycle questions often need the nth event, not just the first or last event.

Schema

orders(order_id INT, user_id INT, order_date TEXT)

Examples

Example 1
Inputuser 1 has Jan 1 and Jan 5; user 2 has Jan 3 only
Output1 -> 2026-01-05

Only user 1 has a second purchase.

Constraints

  • Return the exact requested result.
  • Handle common edge cases.
  • Keep the solution readable before optimizing.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputuser 1 has Jan 1 and Jan 5; user 2 has Jan 3 only
Expected1 -> 2026-01-05

Only user 1 has a second purchase.

Hidden Test Categories
empty or minimal inputrepeated valueslarger realistic input