Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Rolling 30-Day Volatility Query
00:00
5 left

Rolling 30-Day Volatility Query

MediumSQL · PostgreSQL

Problem

Fidelity Investments uses market data analytics to monitor risk for equities available through Fidelity Active Trader Pro. Write a PostgreSQL query that calculates rolling 30-day volatility for the equities in a specified watchlist.

Use daily closing prices to calculate simple daily returns. Treat the 30-day period as the preceding 30 calendar days, including the current observation. Return volatility only when at least two daily returns exist in the rolling window.

Requirements

  1. Join the watchlist to the security master and daily prices.
  2. Calculate each equity's daily return using LAG.
  3. Calculate sample standard deviation of daily returns over the preceding 30 calendar days using a window function.
  4. Return the ticker, price date, daily return, number of returns in the window, and volatility, ordered by ticker and date.

Output details

  • Name the output columns ticker, price_date, daily_return, observations_30d, and volatility_30d in that order.
  • Order results by ticker ascending, then price_date ascending.
  • Exclude rows with null close_price values and treat a zero prior closing price as a null daily_return.
  • Round volatility_30d to six decimal places while retaining full precision for daily_return.

Schema

requested_equities
ColumnTypeDescription
tickerPKVARCHAR(10)Ticker included in the requested watchlist
equities
ColumnTypeDescription
tickerPKVARCHAR(10)Unique equity ticker
security_nameVARCHAR(100)Equity security name
asset_classVARCHAR(30)Security asset classification
daily_prices
ColumnTypeDescription
tickerPKVARCHAR(10)Ticker for the price observation
price_datePKDATETrading date
close_priceNUMERIC(12,4)Unadjusted daily closing price
Tablesrequested_equitiesequitiesdaily_prices
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.