Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Accounts per Strategy

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

Your question is Top Accounts per Strategy. Start with the requirements and the three 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

AllianceBernstein needs a daily trading-performance view for accounts managed through its portfolio platforms. Write a PostgreSQL query that aggregates eligible trades by account and trade date, then identifies the highest-performing accounts within each strategy.

Requirements

  1. Join trades to accounts and strategies.
  2. Include only active accounts and trades from January 1 through January 3, 2025.
  3. Aggregate daily trade count, notional volume, and realized P&L per account.
  4. Calculate each account's total P&L across the date range.
  5. Use a window function to rank accounts within each strategy by total P&L, retaining the top two ranks. Preserve ties.
  6. Return daily rows for qualifying accounts, ordered by strategy, performance rank, date, and account.

Schema

strategies
ColumnTypeDescription
strategy_idPKINTUnique strategy identifier
strategy_nameVARCHAR(100)Display name of the investment strategy
asset_classVARCHAR(50)Primary asset class for the strategy
accounts
ColumnTypeDescription
account_idPKINTUnique portfolio account identifier
strategy_idINTAssigned investment strategy
account_nameVARCHAR(100)Portfolio account name
statusVARCHAR(20)Account lifecycle status
portfolio_trades
ColumnTypeDescription
trade_idPKINTUnique trade identifier
account_idINTAccount executing the trade
trade_dateDATETrade execution date
trade_typeVARCHAR(10)Buy or sell indicator
quantityINTNumber of units traded
notional_valueNUMERIC(14,2)Trade notional value
realized_pnlNUMERIC(14,2)Realized profit or loss from the trade
Tablesstrategiesaccountsportfolio_trades
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results