Compute day-over-day percent change in trading volume for one stock

Quick Overview

Write a SQL query that lists one stock's daily trading volume with its percent change from the previous trading day, rounded to two decimals. It tests the LAG window function, gaps between trading dates, a NULL first row, and avoiding integer division on INT columns.

Compute day-over-day percent change in trading volume for one stock

Company: Robinhood

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

For `stock_id = 1`, return each `trade_date`, `volume`, and `volume_change_pct` versus the previous trading day, ordered by `trade_date` ascending. - `volume_change_pct = ((current_volume - previous_volume) / previous_volume) * 100`, rounded to 2 decimals. - The previous trading day is the latest earlier `trade_date` for the stock (dates may be non-consecutive). - The first record has a NULL change. ```hint Watch the types Check what type `(current_volume - previous_volume) / previous_volume` has when both values are INT. ``` ### Input Table(s) `trades` | Column | Type | Meaning | |---|---|---| | trade_id | INT | Primary key | | stock_id | INT | Stock the row belongs to (NOT NULL) | | trade_date | DATE | Trading day (NOT NULL) | | volume | INT | Shares traded that day, always positive (NOT NULL) | Keys: `trade_id` is the primary key. `(stock_id, trade_date)` is unique. ### Output Contract | Column | Type | Meaning | |---|---|---| | trade_date | DATE | A trading day of stock 1 | | volume | INT | Stock 1's volume on that day | | volume_change_pct | NUMERIC | Percent change from stock 1's previous trading day, rounded to 2 decimals; NULL on the first day | - One row per `trade_date` of `stock_id = 1`. - Order by `trade_date` ascending. The date is unique for one stock. - Round to 2 decimal places, half away from zero (PostgreSQL `ROUND` on a numeric value). ### Example `trades` | trade_id | stock_id | trade_date | volume | |---|---|---|---| | 1 | 1 | 2024-03-01 | 1000 | | 2 | 2 | 2024-03-01 | 500 | | 3 | 1 | 2024-03-04 | 1250 | | 4 | 1 | 2024-03-05 | 1000 | | 5 | 2 | 2024-03-06 | 800 | | 6 | 1 | 2024-03-07 | 3000 | | 7 | 1 | 2024-03-08 | 2999 | Expected result: | trade_date | volume | volume_change_pct | |---|---|---| | 2024-03-01 | 1000 | NULL | | 2024-03-04 | 1250 | 25.00 | | 2024-03-05 | 1000 | -20.00 | | 2024-03-07 | 3000 | 200.00 | | 2024-03-08 | 2999 | -0.03 | The first day has no earlier trade, so its change is NULL. Stock 1 has no row on 2024-03-06 (only stock 2 traded that day), so the previous trading day for 2024-03-07 is 2024-03-05, and the change is (3000 - 1000) / 1000 * 100 = 200.00. On 2024-03-08 the change is -1 / 3000 * 100, about -0.0333, which rounds to -0.03. ### Constraints and Clarifications - `trades` has at most 10,000 rows. - `volume > 0` for all rows, so the previous volume is never zero. No column is NULL. - `(stock_id, trade_date)` is unique, so each day of stock 1 has exactly one previous trading day (except the first). - Gaps between dates (weekends, holidays, missing days) do not matter: the previous trading day is the previous row of stock 1 by date. - Rows of other stocks are ignored, even when they fall between two trading days of stock 1. - The first record of stock 1 returns NULL, not 0.

Overview: Write a SQL query that lists one stock's daily trading volume with its percent change from the previous trading day, rounded to two decimals. It tests the LAG window function, gaps between trading dates, a NULL first row, and avoiding integer division on INT columns.

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

For stock_id = 1, return each trade_date, volume, and volume_change_pct versus the previous trading day, ordered by trade_date ascending.

  • volume_change_pct = ((current_volume - previous_volume) / previous_volume) * 100 , rounded to 2 decimals.
  • The previous trading day is the latest earlier trade_date for the stock (dates may be non-consecutive).
  • The first record has a NULL change.

Input Table(s)

trades

ColumnTypeMeaning
trade_idINTPrimary key
stock_idINTStock the row belongs to (NOT NULL)
trade_dateDATETrading day (NOT NULL)
volumeINTShares traded that day, always positive (NOT NULL)

Keys: trade_id is the primary key. (stock_id, trade_date) is unique.

Output Contract

ColumnTypeMeaning
trade_dateDATEA trading day of stock 1
volumeINTStock 1's volume on that day
volume_change_pctNUMERICPercent change from stock 1's previous trading day, rounded to 2 decimals; NULL on the first day
  • One row per trade_date of stock_id = 1 .
  • Order by trade_date ascending. The date is unique for one stock.
  • Round to 2 decimal places, half away from zero (PostgreSQL ROUND on a numeric value).

Example

trades

trade_idstock_idtrade_datevolume
112024-03-011000
222024-03-01500
312024-03-041250
412024-03-051000
522024-03-06800
612024-03-073000
712024-03-082999

Expected result:

trade_datevolumevolume_change_pct
2024-03-011000NULL
2024-03-04125025.00
2024-03-051000-20.00
2024-03-073000200.00
2024-03-082999-0.03

The first day has no earlier trade, so its change is NULL. Stock 1 has no row on 2024-03-06 (only stock 2 traded that day), so the previous trading day for 2024-03-07 is 2024-03-05, and the change is (3000 - 1000) / 1000 * 100 = 200.00. On 2024-03-08 the change is -1 / 3000 * 100, about -0.0333, which rounds to -0.03.

Constraints and Clarifications

  • trades has at most 10,000 rows.
  • volume > 0 for all rows, so the previous volume is never zero. No column is NULL.
  • (stock_id, trade_date) is unique, so each day of stock 1 has exactly one previous trading day (except the first).
  • Gaps between dates (weekends, holidays, missing days) do not matter: the previous trading day is the previous row of stock 1 by date.
  • Rows of other stocks are ignored, even when they fall between two trading days of stock 1.
  • The first record of stock 1 returns NULL, not 0.