Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
Start free
Find Active Loans with Unsuccessful Latest Payments
00:00
5 left

Find Active Loans with Unsuccessful Latest Payments

MediumSQL · PostgreSQL

Problem

Explain how you would optimize a slow-running database query that handles millions of customer records at PayJoy.

For the practical portion, provide a PostgreSQL query that returns active customers with active loans, a positive outstanding balance, and no successful latest payment. Assume the query must remain correct at large scale.

Output

  1. One row per qualifying loan, with customer_id, full_name, loan_id, outstanding_balance, latest_payment_date, and latest_payment_status
  2. Include only active customers and loans with a positive balance whose latest payment is missing or not successful
  3. Order by outstanding_balance descending, then customer_id ascending

Output details

  • Compute outstanding_balance as principal_amount minus the sum of amount for payments with status equal to successful.
  • Treat loans without successful payments as having a successful-payment total of 0.
  • Define the latest payment by greatest payment_date, breaking ties with greatest payment_id.
  • Return NULL for latest_payment_date and latest_payment_status when a loan has no payments.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique PayJoy customer identifier
full_nameVARCHAR(120)Customer's full name
statusVARCHAR(20)Customer account status
loans
ColumnTypeDescription
loan_idPKINTUnique loan identifier
customer_idINTCustomer who owns the loan
principal_amountNUMERIC(12,2)Original loan principal
statusVARCHAR(20)Current loan status
payments
ColumnTypeDescription
payment_idPKINTUnique payment identifier
loan_idINTLoan associated with the payment
payment_dateDATEDate the payment was recorded
amountNUMERIC(12,2)Payment amount
statusVARCHAR(20)Payment processing status
Tablescustomersloanspayments
Interviewer

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

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.