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
SQL for Risk-Adjusted Top Portfolios
00:00
5 left

SQL for Risk-Adjusted Top Portfolios

HardSQL · PostgreSQL

Problem

Write a SQL query to identify the top-performing portfolios over a rolling three-month window, accounting for risk-adjusted returns.

Use the provided monthly portfolio returns and monthly risk-free rates. A portfolio qualifies only when three consecutive monthly observations are available. Return the top two portfolios for each eligible month.

Output

  1. One row per qualifying portfolio and month, with month_end, portfolio_id, portfolio_name, rolling_return, rolling_volatility, risk_adjusted_return, and performance_rank.
  2. Use average monthly return divided by sample standard deviation of monthly excess returns as the risk-adjusted return.
  3. Sort by month_end ascending, then performance_rank ascending, then portfolio_id ascending.

Schema

portfolios
ColumnTypeDescription
portfolio_idPKINTUnique portfolio identifier
portfolio_nameVARCHAR(100)Portfolio display name
risk_profileVARCHAR(20)Portfolio risk classification
portfolio_monthly_returns
ColumnTypeDescription
portfolio_idINTReferences portfolios.portfolio_id
month_endDATEMonth-end return date
return_pctNUMERIC(8,5)Monthly total return as a decimal
risk_free_rates
ColumnTypeDescription
month_endPKDATEMonth-end benchmark date
rate_pctNUMERIC(8,5)Monthly risk-free rate as a decimal
Tablesportfoliosportfolio_monthly_returnsrisk_free_rates
Interviewer

Your question is SQL for Risk-Adjusted Top Portfolios. Start with the requirements and the three 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.