Top 5 stocks by total traded volume in a one-minute window
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Find the top 5 stocks by total `volume` (summed per `symbol` across all exchanges) for trades with `trade_time` from `2024-06-01 09:30:00` (inclusive) to `2024-06-01 09:31:00` (exclusive). Return `symbol` and `TotalVolume` ordered by `TotalVolume` descending, then `symbol` ascending, at most 5 rows (all stocks if fewer than 5 traded).
```hint Window edges
Check which comparison operators keep a trade stamped exactly `09:31:00` out of the result and keep one stamped exactly `09:30:00` in.
```
### Input Table(s)
`trades` (one row per trade, primary key `trade_id`)
| Column | Type | Meaning |
|---|---|---|
| trade_id | INT | Primary key |
| symbol | VARCHAR(10) | Stock symbol, NOT NULL |
| exchange | VARCHAR(20) | Exchange where the trade executed, NOT NULL |
| volume | INT | Shares traded, NOT NULL |
| price | DECIMAL(10,2) | Trade price, NOT NULL |
| trade_time | TIMESTAMP | Execution time without a time zone (`DATETIME` in MySQL), NOT NULL |
### Output Contract
- Columns, in this order: `symbol`, `TotalVolume`. The second column name is case-sensitive, exactly `TotalVolume`.
- One row per symbol that traded in the window, with `TotalVolume` equal to the sum of `volume` over that symbol's trades in the window, across all exchanges.
- Order by `TotalVolume` descending, then `symbol` ascending.
- Return at most 5 rows: the first 5 in that order, or every symbol if fewer than 5 traded in the window.
### Example
`trades`
| trade_id | symbol | exchange | volume | price | trade_time |
|---|---|---|---|---|---|
| 1 | AAPL | NASDAQ | 300 | 192.10 | 2024-06-01 09:30:00 |
| 2 | AAPL | NYSE | 200 | 192.12 | 2024-06-01 09:30:45 |
| 3 | MSFT | NASDAQ | 500 | 415.30 | 2024-06-01 09:30:10 |
| 4 | TSLA | NASDAQ | 900 | 178.05 | 2024-06-01 09:31:00 |
| 5 | TSLA | CBOE | 150 | 178.00 | 2024-06-01 09:30:59 |
| 6 | NVDA | NYSE | 1000 | 120.50 | 2024-06-01 09:29:59 |
| 7 | NVDA | NASDAQ | 400 | 120.55 | 2024-06-01 09:30:30 |
| 8 | AMZN | NASDAQ | 250 | 180.40 | 2024-06-01 09:30:20 |
| 9 | GOOG | NASDAQ | 100 | 175.00 | 2024-06-01 09:30:05 |
| 10 | META | NYSE | 150 | 470.00 | 2024-06-01 09:30:50 |
Expected result:
| symbol | TotalVolume |
|---|---|
| AAPL | 500 |
| MSFT | 500 |
| NVDA | 400 |
| AMZN | 250 |
| META | 150 |
Trade 1 sits on the inclusive start and counts, so AAPL totals 300 + 200 = 500 across two exchanges. Trade 4 (09:31:00) and trade 6 (09:29:59) fall outside the window, leaving TSLA with 150 and NVDA with 400. AAPL and MSFT tie at 500 and sort by symbol. META and TSLA tie at 150 for fifth place, and META comes first alphabetically, so TSLA and GOOG (100) are cut.
### Constraints and Clarifications
- There are between 1 and 100,000 trades.
- `volume` is a positive integer.
- `symbol` contains only uppercase letters, 1 to 10 characters.
- No column contains NULL.
- `trade_time` has no time zone. Compare it directly with the window bounds as written.
- A symbol's total adds up its trades on every exchange. Each row is a separate trade, so repeated trades of the same symbol all count.
- Ties at the fifth-place cut are settled by `symbol`, so the result never has more than 5 rows. If no trades fall in the window, the result is empty.
Overview: Write a SQL query that sums trade volume per stock symbol across exchanges inside a one-minute window and returns the five highest totals. It tests half-open time filters, GROUP BY with SUM, deterministic tiebreaking at a top-N cut, and LIMIT.