Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Optimize Complex SQL Joins
00:00
5 left

Optimize Complex SQL Joins

HardSQL · PostgreSQL

Problem

How do you optimize a slow-running SQL query containing multiple complex joins?

Assume the provided banking tables contain customers, accounts, transactions, loan applications, and loan payments. Write an optimized PostgreSQL query that returns active customers with active accounts, their in-period transaction totals, latest loan details, and payment totals. Avoid duplicate monetary totals caused by one-to-many joins.

Output

  1. One row per active customer with at least one active account.
  2. Columns: customer_id, customer_name, active_account_count, transaction_total, latest_loan_id, latest_loan_amount, loan_payment_total.
  3. Include customers without loans or in-period transactions, using zero totals where appropriate.
  4. Sort by customer_id ascending.

Schema

customers
ColumnTypeDescription
customer_idPKINTCustomer identifier
customer_nameVARCHAR(100)Customer name
customer_statusVARCHAR(20)Customer lifecycle status
accounts
ColumnTypeDescription
account_idPKINTAccount identifier
customer_idINTOwning customer
account_typeVARCHAR(30)Bank account type
account_statusVARCHAR(20)Account lifecycle status
transactions
ColumnTypeDescription
transaction_idPKINTTransaction identifier
account_idINTAccount involved in the transaction
transaction_dateDATETransaction posting date
amountNUMERIC(12,2)Transaction amount
loan_applications
ColumnTypeDescription
loan_application_idPKINTLoan application identifier
customer_idINTApplicant customer
application_dateDATELoan application date
loan_amountNUMERIC(12,2)Requested or approved loan amount
loan_statusVARCHAR(20)Loan application status
loan_payments
ColumnTypeDescription
payment_idPKINTPayment identifier
loan_application_idINTRelated loan application
payment_dateDATEPayment date
payment_amountNUMERIC(12,2)Payment amount
Tablescustomersaccountstransactionsloan_applicationsloan_payments
Interviewer

Your question is Optimize Complex SQL Joins. Start with the requirements and the five 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.