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 Multi-Table Join Query
00:00
5 left

Optimize Multi-Table Join Query

MediumSQL · PostgreSQL

Problem

How do you optimize a slow-running SQL query that joins multiple large tables containing policyholder data?

Use the provided PostgreSQL tables to return metrics for policyholders with at least one active policy. Include only open claims filed during 2025. The result should avoid duplicate counting when a policyholder has multiple policies or claims.

Output

  1. One row per policyholder with an active policy
  2. Columns: policyholder_id, full_name, active_policy_count, open_claim_count, total_incurred
  3. Sort by policyholder_id ascending

Schema

policyholders
ColumnTypeDescription
policyholder_idPKINTUnique policyholder identifier
full_nameVARCHAR(120)Policyholder full name
state_codeVARCHAR(2)Mailing state abbreviation
policies
ColumnTypeDescription
policy_idPKINTUnique policy identifier
policyholder_idINTPolicyholder owning the policy
policy_typeVARCHAR(30)Type of insurance policy
policy_statusVARCHAR(20)Current policy status
claims
ColumnTypeDescription
claim_idPKINTUnique claim identifier
policy_idINTPolicy associated with the claim
claim_statusVARCHAR(20)Current claim status
filed_atDATEDate the claim was filed
incurred_amountNUMERIC(12,2)Incurred claim amount
Tablespolicyholderspoliciesclaims
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.

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.