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.