9/34
Part 2 of 6 · RankingLearn and do

PARTITION BY restarts the numbering in each group

Each user's orders, oldest first:

SELECT user_id, order_id, ordered_on,
       ROW_NUMBER() OVER (PARTITION BY user_id
                          ORDER BY ordered_on, order_id) AS rn
FROM orders
ORDER BY user_id, rn;
user_idorder_idordered_onrn
112026-03-011
162026-03-022
132026-03-033
222026-03-011
252026-03-092
342026-03-031
472026-03-101

Each user starts again at 1, and rn = 1 is each user's first order. Sort by the amount, largest first, and rn = 1 is each user's largest order instead. User 1 has two orders of 45, so the tie-break decides which one.

In the editor, finish LARGEST_ORDER: each user's largest order, ties to the lower order_id.