Premium problem38. Best Seller by Month

Medium Locked

order_items(order_id, product_id, quantity, revenue, ordered_at)

For each month of 2026, return the product or products with the highest total quantity sold, keeping every tie.

Return month as 'YYYY-MM', plus product_id and units_sold, ordered by month then product.

Quantity, not revenue, decides the winner, and a product's quantity may be split across several rows in the same month.

Tables

order_items

order_id  product_id  quantity  revenue  ordered_at
--------  ----------  --------  -------  ----------
1         1           3         30.00    2026-01-05
2         1           2         20.00    2026-01-20
3         2           5         50.00    2026-01-10
4         3           1         9999.00  2026-01-12
5         4           7         70.00    2026-02-02
6         5           2         20.00    2026-02-03
7         9           999       999.00   2025-01-01

Expected result

month    product_id  units_sold
-------  ----------  ----------
2026-01  1           5
2026-01  2           5
2026-02  4           7

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query