Find each ticker's highest and lowest 2023 closing prices and their dates

Quick Overview

Write a SQL query that finds each stock ticker's highest and lowest closing price in 2023, along with the trade dates on which they occurred. It tests per-group aggregation, joining back to recover the dates, filtering by year, and returning every date combination when several days tie for an extreme price.

Find each ticker's highest and lowest 2023 closing prices and their dates

Company: Robinhood

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

For each `ticker`, find the highest and lowest `close_price` in 2023 and the `trade_date` of each. Return `ticker`, `highest_date`, `highest_price`, `lowest_date`, `lowest_price`. - Only consider rows with `trade_date` in 2023. - If multiple dates share an extreme price, return all highest/lowest date combinations. - Order by `ticker`, then `highest_date`, then `lowest_date` (all ascending). ```hint Prices versus dates A plain `GROUP BY ticker` with `MAX` and `MIN` gives you the extreme prices, but not the days they happened on. ``` ### Input Table(s) `stock_prices` | Column | Type | Meaning | |---|---|---| | id | INT | Primary key (auto-increment) | | ticker | VARCHAR(10) | Stock symbol (NOT NULL) | | trade_date | DATE | Trading day (NOT NULL) | | close_price | DECIMAL(10,2) | Closing price on that day, positive (NOT NULL) | Keys: `id` is the primary key. A ticker has at most one row per `trade_date`. ### Output Contract | Column | Type | Meaning | |---|---|---| | ticker | VARCHAR(10) | Stock symbol with at least one row in 2023 | | highest_date | DATE | A 2023 date on which the ticker closed at its 2023 maximum | | highest_price | DECIMAL(10,2) | The ticker's highest 2023 `close_price` | | lowest_date | DATE | A 2023 date on which the ticker closed at its 2023 minimum | | lowest_price | DECIMAL(10,2) | The ticker's lowest 2023 `close_price` | - One row for every combination of a highest date and a lowest date of the same ticker. - Prices are returned as stored, with no rounding. - Order by `ticker`, `highest_date`, `lowest_date`, all ascending. These three columns identify a row. ### Example `stock_prices` | id | ticker | trade_date | close_price | |---|---|---|---| | 1 | AAPL | 2022-12-30 | 198.11 | | 2 | AAPL | 2023-01-03 | 125.07 | | 3 | AAPL | 2023-06-15 | 186.01 | | 4 | AAPL | 2023-12-14 | 198.11 | | 5 | AAPL | 2024-01-02 | 120.00 | | 6 | META | 2023-02-01 | 175.00 | | 7 | META | 2023-04-05 | 210.50 | | 8 | META | 2023-07-20 | 300.00 | | 9 | META | 2023-09-12 | 300.00 | | 10 | META | 2023-11-01 | 175.00 | | 11 | NFLX | 2023-05-05 | 330.00 | Expected result: | ticker | highest_date | highest_price | lowest_date | lowest_price | |---|---|---|---|---| | AAPL | 2023-12-14 | 198.11 | 2023-01-03 | 125.07 | | META | 2023-07-20 | 300.00 | 2023-02-01 | 175.00 | | META | 2023-07-20 | 300.00 | 2023-11-01 | 175.00 | | META | 2023-09-12 | 300.00 | 2023-02-01 | 175.00 | | META | 2023-09-12 | 300.00 | 2023-11-01 | 175.00 | | NFLX | 2023-05-05 | 330.00 | 2023-05-05 | 330.00 | AAPL's 2023 rows run from 125.07 to 198.11. The 2022 row at the same 198.11 and the lower 2024 row are outside the year and do not appear. META hits its high of 300.00 on two days and its low of 175.00 on two days, so it gets all four date pairs. NFLX has a single 2023 row, which is both its high and its low. ### Constraints and Clarifications - The table has at most 10,000 rows. - `ticker` is one of `'META'`, `'AAPL'`, `'AMZN'`, `'NFLX'`, `'GOOG'`. - `trade_date` is between 2020-01-01 and 2025-12-31, and `close_price` is positive. No column is NULL. - A ticker with no 2023 rows does not appear in the result. - A ticker with one 2023 row, or with the same price on every 2023 day, still appears. Its highest and lowest dates can then be the same day. - Ties on an extreme price are kept: with ${h}$ highest dates and ${l}$ lowest dates, a ticker contributes ${h \cdot l}$ rows.

Overview: Write a SQL query that finds each stock ticker's highest and lowest closing price in 2023, along with the trade dates on which they occurred. It tests per-group aggregation, joining back to recover the dates, filtering by year, and returning every date combination when several days tie for an extreme price.

Robinhood logo
Robinhood
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

For each ticker, find the highest and lowest close_price in 2023 and the trade_date of each. Return ticker, highest_date, highest_price, lowest_date, lowest_price.

  • Only consider rows with trade_date in 2023.
  • If multiple dates share an extreme price, return all highest/lowest date combinations.
  • Order by ticker , then highest_date , then lowest_date (all ascending).

Input Table(s)

stock_prices

ColumnTypeMeaning
idINTPrimary key (auto-increment)
tickerVARCHAR(10)Stock symbol (NOT NULL)
trade_dateDATETrading day (NOT NULL)
close_priceDECIMAL(10,2)Closing price on that day, positive (NOT NULL)

Keys: id is the primary key. A ticker has at most one row per trade_date.

Output Contract

ColumnTypeMeaning
tickerVARCHAR(10)Stock symbol with at least one row in 2023
highest_dateDATEA 2023 date on which the ticker closed at its 2023 maximum
highest_priceDECIMAL(10,2)The ticker's highest 2023 close_price
lowest_dateDATEA 2023 date on which the ticker closed at its 2023 minimum
lowest_priceDECIMAL(10,2)The ticker's lowest 2023 close_price
  • One row for every combination of a highest date and a lowest date of the same ticker.
  • Prices are returned as stored, with no rounding.
  • Order by ticker , highest_date , lowest_date , all ascending. These three columns identify a row.

Example

stock_prices

idtickertrade_dateclose_price
1AAPL2022-12-30198.11
2AAPL2023-01-03125.07
3AAPL2023-06-15186.01
4AAPL2023-12-14198.11
5AAPL2024-01-02120.00
6META2023-02-01175.00
7META2023-04-05210.50
8META2023-07-20300.00
9META2023-09-12300.00
10META2023-11-01175.00
11NFLX2023-05-05330.00

Expected result:

tickerhighest_datehighest_pricelowest_datelowest_price
AAPL2023-12-14198.112023-01-03125.07
META2023-07-20300.002023-02-01175.00
META2023-07-20300.002023-11-01175.00
META2023-09-12300.002023-02-01175.00
META2023-09-12300.002023-11-01175.00
NFLX2023-05-05330.002023-05-05330.00

AAPL's 2023 rows run from 125.07 to 198.11. The 2022 row at the same 198.11 and the lower 2024 row are outside the year and do not appear. META hits its high of 300.00 on two days and its low of 175.00 on two days, so it gets all four date pairs. NFLX has a single 2023 row, which is both its high and its low.

Constraints and Clarifications

  • The table has at most 10,000 rows.
  • ticker is one of 'META' , 'AAPL' , 'AMZN' , 'NFLX' , 'GOOG' .
  • trade_date is between 2020-01-01 and 2025-12-31, and close_price is positive. No column is NULL.
  • A ticker with no 2023 rows does not appear in the result.
  • A ticker with one 2023 row, or with the same price on every 2023 day, still appears. Its highest and lowest dates can then be the same day.
  • Ties on an extreme price are kept: with h{h} highest dates and l{l} lowest dates, a ticker contributes h⋅l{h \cdot l} rows.