Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Ranking Within Branches
00:00
5 left

SQL Ranking Within Branches

MediumSQL · PostgreSQL

Problem

Write SQL to rank customers by loan amount within each branch.

Calculate each customer's total non-null loan amount and assign the same rank to customers tied at the same amount. Include only customers with at least one non-null loan associated with a branch.

Output

  1. One row per included customer with branch_name, customer_id, customer_name, total_loan_amount, and branch_rank.
  2. Sort by branch name, rank ascending, then customer ID ascending.

Schema

branches
ColumnTypeDescription
branch_idPKINTUnique branch identifier
branch_nameVARCHAR(100)Branch name
customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(150)Customer full name
branch_idINTBranch assigned to the customer
loans
ColumnTypeDescription
loan_idPKINTUnique loan identifier
customer_idINTCustomer receiving the loan
loan_amountNUMERIC(12,2)Loan principal amount
Tablesbranchescustomersloans
Interviewer

Your question is SQL Ranking Within Branches. 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.