Premium problem37. Monthly Stock High and Low

Medium Locked

stock_prices(symbol, price_date, close_price)

For each symbol and month, return the date and price of the highest close and of the lowest close. Where a price ties, take the earliest date.

Return symbol, month as 'YYYY-MM', high_date, high_price, low_date and low_price, ordered by symbol then month.

MAX(close_price) alone gives the price but not the date it happened on, which is the part that needs ranking.

Tables

stock_prices

symbol  price_date  close_price
------  ----------  -----------
ACME    2026-01-05  110.00
ACME    2026-01-10  90.00
ACME    2026-01-20  110.00
ACME    2026-02-02  75.00
BORN    2026-01-07  50.00
BORN    2026-01-08  60.00

Expected result

symbol  month    high_date   high_price  low_date    low_price
------  -------  ----------  ----------  ----------  ---------
ACME    2026-01  2026-01-05  110.00      2026-01-10  90.00
ACME    2026-02  2026-02-02  75.00       2026-02-02  75.00
BORN    2026-01  2026-01-08  60.00       2026-01-07  50.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