Premium problem56. Median Order Value by Country

Hard Locked

orders(order_id, country, total_amount, ordered_at)

For orders placed in 2026, return the median total_amount per country.

With an odd number of orders the median is the middle value. With an even number it is the average of the two middle values.

Return country and median_order_amount rounded to two decimals, ordered by country.

There is no median aggregate here. Number the rows within each country, work out which one or two positions sit in the middle, and average them.

Tables

orders

order_id  country  total_amount  ordered_at
--------  -------  ------------  ----------
1         IN       10.00         2026-01-01
2         IN       20.00         2026-01-02
3         IN       30.00         2026-01-03
4         IN       400.00        2026-01-04
5         US       5.00          2026-02-01
6         US       7.00          2026-02-02
7         US       900.00        2026-02-03
8         US       1.00          2025-12-31

Expected result

country  median_order_amount
-------  -------------------
IN       25.00
US       7.00

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