Daily return

window functions

Daily return

Coinbase SQL Interview Question

Coinbase shows a percentage move beside every asset, and it has to be measured against that asset's own previous close. Against a fixed starting price, a coin that fell last week would still look like it is falling today, which is how people make bad decisions with real money.

Return the symbol, the priced_on date, the close_usd price, and the percentage change from that asset's previous close as pct_change, rounded to 2 decimal places. The first day of each asset has nothing before it, so leave its change empty. Sort by symbol and then by priced_on.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

coinbase_daily_pricesTable24 rows

Column NameType
symbolVARCHAR
priced_onDATE
close_usdDOUBLE

coinbase_daily_pricesExample Input

symbolpriced_onclose_usd
BTC2024-05-0158000
BTC2024-05-0259200
BTC2024-05-0360500
BTC2024-05-0461100
BTC2024-05-0559800
ETH2024-05-012900
ETH2024-05-022860
ETH2024-05-032945
ETH2024-05-043010
ETH2024-05-053080

Example Output

symbolpriced_onclose_usdpct_change
BTC2024-05-0158000NULL
BTC2024-05-02592002.07
BTC2024-05-03605002.2
BTC2024-05-04611000.99
BTC2024-05-0559800-2.13
ETH2024-05-012900NULL
ETH2024-05-022860-1.38
ETH2024-05-0329452.97
ETH2024-05-0430102.21
ETH2024-05-0530802.33

Explanation

BTC opens the example at 58000 with nothing before it, so its change is empty. The next day at 59200 is 2.07 percent higher, and the day after that is 2.2 percent higher again. ETH starts its own comparison at 2900 rather than carrying on from BTC.

The example above is a small slice of the data. Your query runs against the full tables.