Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
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 feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Optimizing a Poor SQL Query
00:00
5 left

Optimizing a Poor SQL Query

HardSQL · PostgreSQL

Problem

How do you optimize a complex SQL query that is performing poorly?

Using the provided PostgreSQL tables, write an optimized query that returns broker performance for active policies starting in 2025. The result should preserve the required filters, aggregations, and inclusion of brokers without matching claims.

Output

  1. One row per qualifying broker, with broker_id, broker_name, active_policy_count, total_premium, claim_count, and total_claim_amount.
  2. Include only brokers whose 2025 active-policy premium exceeds 1,000.
  3. Sort by total_premium descending, then broker_id ascending.

Schema

brokers
ColumnTypeDescription
broker_idPKINTUnique broker identifier
broker_nameVARCHAR(100)Broker display name
regionVARCHAR(50)Broker operating region
policies
ColumnTypeDescription
policy_idPKINTUnique policy identifier
broker_idINTBroker responsible for the policy
statusVARCHAR(20)Policy lifecycle status
start_dateDATEPolicy start date
premiumNUMERIC(12,2)Policy premium amount
claims
ColumnTypeDescription
claim_idPKINTUnique claim identifier
policy_idINTPolicy associated with the claim
claim_dateDATEDate the claim was filed
claim_amountNUMERIC(12,2)Claim amount
Tablesbrokerspoliciesclaims
Interviewer

Your question is Optimizing a Poor SQL Query. 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.