
Three day average
mediumThree day average
Coinbase SQL Interview Question
Coinbase smooths the price line on its charts so one volatile hour does not read as a trend reversal. The smoothing is a three day average ending on the day being drawn, and it has to start from the asset's very first day rather than three days late.
Return the symbol, the priced_on date, the close_usd price, and the average close over that day and the two before it as three_day_avg, rounded to 2 decimal places. On an asset's first two days, average whatever days exist. 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 Name | Type |
|---|---|
| symbol | VARCHAR |
| priced_on | DATE |
| close_usd | DOUBLE |
coinbase_daily_pricesExample Input
| symbol | priced_on | close_usd |
|---|---|---|
| BTC | 2024-05-01 | 58000 |
| BTC | 2024-05-02 | 59200 |
| BTC | 2024-05-03 | 60500 |
| BTC | 2024-05-04 | 61100 |
| BTC | 2024-05-05 | 59800 |
| ETH | 2024-05-01 | 2900 |
| ETH | 2024-05-02 | 2860 |
| ETH | 2024-05-03 | 2945 |
| ETH | 2024-05-04 | 3010 |
| ETH | 2024-05-05 | 3080 |
Example Output
| symbol | priced_on | close_usd | three_day_avg |
|---|---|---|---|
| BTC | 2024-05-01 | 58000 | 58000 |
| BTC | 2024-05-02 | 59200 | 58600 |
| BTC | 2024-05-03 | 60500 | 59233.33 |
| BTC | 2024-05-04 | 61100 | 60266.67 |
| BTC | 2024-05-05 | 59800 | 60466.67 |
| ETH | 2024-05-01 | 2900 | 2900 |
| ETH | 2024-05-02 | 2860 | 2880 |
| ETH | 2024-05-03 | 2945 | 2901.67 |
| ETH | 2024-05-04 | 3010 | 2938.33 |
| ETH | 2024-05-05 | 3080 | 3011.67 |
Explanation
On BTC's first day there is only one price to average, so the figure is 58000. On the second day it averages two days and gives 58600. From the third day on it averages three, so 58000, 59200 and 60500 come out at 59233.33.
The example above is a small slice of the data. Your query runs against the full tables.
Company
Coinbase
Difficulty
medium
Topic
window functions
Language
SQL
Your query
Starting the SQL engine...