Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rank Rows Within Each Group

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

Your question is Rank Rows Within Each Group. 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

RBC branch operations wants to identify the highest-balance active accounts at each branch. Write a PostgreSQL query that joins account records to RBC branches and ranks eligible accounts within each branch using a window function.

Requirements

  1. Include only accounts whose status is Active and whose current_balance is not NULL.
  2. Use RANK() with PARTITION BY branch_id, ordering balances from highest to lowest.
  3. Preserve ties, so accounts with equal balances receive the same rank.
  4. Return branch details, account details, balance, and rank, ordered by branch and rank.

Schema

branches
ColumnTypeDescription
branch_idPKINTEGERUnique RBC branch identifier
branch_nameVARCHAR(100)RBC branch name
provinceVARCHAR(50)Canadian province containing the branch
accounts
ColumnTypeDescription
account_idPKINTEGERUnique account identifier
branch_idINTEGERRBC branch associated with the account
account_typeVARCHAR(30)Account product type
statusVARCHAR(20)Current account status
current_balanceNUMERIC(12,2)Current balance of the account
Tablesbranchesaccounts
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results