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 SQL for Large Tables
00:00
5 left

Optimize SQL for Large Tables

MediumSQL · PostgreSQL

Problem

How do you optimize a SQL SELECT statement for a large table?

Use the supplied PostgreSQL tables to return active records whose latest non-test event occurred during 2025. Preserve active records even when no matching label exists, and avoid applying functions to the timestamp predicate.

Output

  1. One row per qualifying active record with record_id, record_name, label_name, latest_event_type, and latest_event_at.
  2. Include records with no label, order by latest_event_at descending, then record_id ascending.

Schema

records
ColumnTypeDescription
record_idPKINTRecord identifier
record_nameVARCHAR(100)Record name
statusVARCHAR(20)Record lifecycle status
record_events
ColumnTypeDescription
event_idPKINTEvent identifier
record_idINTReferenced record
event_typeVARCHAR(30)Event classification
event_tsTIMESTAMPEvent timestamp
record_labels
ColumnTypeDescription
label_idPKINTLabel identifier
record_idINTReferenced record
label_nameVARCHAR(60)Optional label name
Tablesrecordsrecord_eventsrecord_labels
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.