Longest rising streak

window functions

Longest rising streak

Coinbase SQL Interview Question

The line readers quote back from Coinbase's weekly market note is the one about momentum: how many days in a row an asset closed higher than the day before. A single large jump is noise. Five consecutive green days is a trend worth a sentence.

For each asset, return the symbol and the length of its longest unbroken run of days that closed higher than the previous day as rising_days. Count the run as the number of rising days in it.

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

symbolrising_days
BTC3
ETH3

Explanation

BTC rose on three days in a row in the example before turning down, so its longest run is 3. ETH fell on its second day and then rose on each of the three days after it, which is also a run of 3.

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