Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.