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
Optimize a SQL Query
00:00
5 left

Optimize a SQL Query

HardSQL · PostgreSQL

Problem

LexisNexis Risk Solutions runs this query from an internal fraud-review dashboard to pull flagged risk reports for name-matched entities. It used to return in under a second; now, as the risk_reports table has grown past tens of millions of rows, analysts wait 20-30 seconds every time they load the dashboard.

-- entities(id INT PRIMARY KEY, full_name TEXT, ssn_last4 VARCHAR(4), state VARCHAR(2))
-- risk_reports(id INT PRIMARY KEY, entity_id INT, created_at TIMESTAMP, risk_score INT, status VARCHAR(20))
-- indexes: risk_reports(entity_id), risk_reports(created_at)

SELECT *
FROM risk_reports r
WHERE DATE(r.created_at) = '2026-09-01'
  AND r.status = 7
  AND r.entity_id IN (
    SELECT e.id FROM entities e WHERE e.full_name LIKE '%smith%'
  )
ORDER BY r.risk_score DESC;

How would you optimize this query? Walk through what's forcing a full scan today, table by table, and what you'd change so the existing indexes on risk_reports(entity_id) and risk_reports(created_at) can actually be used.

Schema

entities
ColumnTypeDescription
idPKINTEntity identifier
full_nameTEXTEntity full name
ssn_last4VARCHAR(4)Last four digits of the SSN
stateVARCHAR(2)Two-letter state code
risk_reports
ColumnTypeDescription
idPKINTRisk report identifier
entity_idINTReferenced entity identifier
created_atTIMESTAMPReport creation timestamp
risk_scoreINTCalculated risk score
statusVARCHAR(20)Report workflow status
Tablesentitiesrisk_reports
Interviewer

Your question is Optimize a SQL Query. Start with the requirements and the two 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.