Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Aggregate Daily Returns Query

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Aggregate Daily Returns Query. Start with the requirements and the two tables on the right.

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.

Problem

Aqr's portfolio tracking system stores portfolio metadata separately from daily return observations. Write a PostgreSQL query that summarizes active portfolios between 2025-01-01 and 2025-01-03.

Requirements

  1. Join portfolio metadata to return observations and exclude inactive portfolios.
  2. Return one row per portfolio and return date, with the summed daily return percentage.
  3. Include the number of non-null return observations and the number of positive observations.
  4. Sort results by return date ascending, then portfolio name ascending. Treat a day containing only null return values as a zero aggregate return.

Schema

portfolios
ColumnTypeDescription
portfolio_idPKINTUnique portfolio identifier
portfolio_nameVARCHAR(100)Aqr portfolio name
portfolio_typeVARCHAR(50)Portfolio strategy classification
is_activeBOOLEANWhether the portfolio is currently active
portfolio_return_observations
ColumnTypeDescription
observation_idPKINTUnique return observation identifier
portfolio_idINTPortfolio associated with the observation
return_dateDATEDate of the return observation
return_pctDECIMAL(10,6)Return percentage for the observation
Tablesportfoliosportfolio_return_observations
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results