Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Rolling 30-Day Asset Average
00:00
5 left

Rolling 30-Day Asset Average

MediumSQL · PostgreSQL

Problem

BlackRock's Aladdin risk analytics workflow needs a time-series view of recent asset performance. Given daily closing prices and an in-scope asset set, write a PostgreSQL query that calculates each asset's daily return and rolling 30-calendar-day average return.

Requirements

  1. Restrict the analysis to assets in portfolio BLK_CORE where is_in_scope = TRUE.
  2. Calculate each daily return as the current closing price divided by the previous available closing price minus one, using LAG partitioned by asset.
  3. Calculate the rolling average over the current date and preceding 29 calendar days, not merely the preceding 30 rows.
  4. Preserve the first price observation for each asset, returning NULL for its daily return and rolling average.
  5. Return results ordered by ticker and price date, with returns rounded to six decimal places.

Schema

assets
ColumnTypeDescription
asset_idPKINTUnique asset identifier
tickerVARCHAR(12)Trading symbol
asset_nameVARCHAR(100)Asset name
portfolio_codeVARCHAR(30)Portfolio membership code
is_in_scopeBOOLEANWhether the asset is included in the analysis
asset_prices
ColumnTypeDescription
price_idPKINTUnique price observation identifier
asset_idINTReferences assets.asset_id
price_dateDATEClosing-price date
close_priceNUMERIC(12,4)Closing price
Tablesassetsasset_prices
Interviewer

Your question is Rolling 30-Day Asset Average. Start with the requirements and the two tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.