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
ExperiencesCoaching
New
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
Get started
Optimize PostgreSQL Joins Without Duplicate Totals
00:00
5 left

Optimize PostgreSQL Joins Without Duplicate Totals

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.

Output details

  • Filter customers and accounts where their respective status equals ACTIVE.
  • Use 2025-01-01 inclusive through 2025-04-01 exclusive for both transaction and payment dates.
  • Choose the latest loan by application_date descending, breaking ties by loan_application_id descending.
  • Leave latest_loan_id and latest_loan_amount NULL when the customer has no loan.
  • Consider all loan applications when selecting the latest loan, regardless of loan status.

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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the five tables are in the Question tab.

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.