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
Rewrite Risk Report Query for Index Usage
00:00
5 left

Rewrite Risk Report Query for Index Usage

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.

Output details

  • Filter created_at inclusively from TIMESTAMP '2026-09-01 00:00:00' and exclusively before TIMESTAMP '2026-09-02 00:00:00'.
  • Sort descending by risk_score, then ascending by id to break ties.

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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two 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.