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
Optimize Timeout-Prone Event Log SQL
00:00
5 left

Optimize Timeout-Prone Event Log SQL

HardSQL · PostgreSQL

Problem

How do you optimize a complex SQL query that joins multiple massive event logs and times out in production?

Write a PostgreSQL query that returns the requested production requests for the specified date range and combines their model, tool, and safety activity. The result must preserve requests that have no matching activity.

Output

  1. One row per completed production request from October 1 through October 5, 2026, inclusive.
  2. Columns: request_id, workspace_id, total_output_tokens, model_event_count, tool_call_count, blocked_event_count, and last_tool_at.
  3. Include zero counts where no activity exists and sort by started_at, then request_id.

Schema

request_log
ColumnTypeDescription
request_idPKVARCHAR(20)Unique request identifier
workspace_idVARCHAR(20)Workspace that submitted the request
started_atTIMESTAMPTZRequest start timestamp
environmentVARCHAR(20)Execution environment
statusVARCHAR(20)Request completion status
model_event_log
ColumnTypeDescription
model_event_idPKINTUnique model event identifier
request_idVARCHAR(20)Associated request
event_atTIMESTAMPTZModel event timestamp
output_tokensINTTokens generated by the event
tool_event_log
ColumnTypeDescription
tool_event_idPKINTUnique tool event identifier
request_idVARCHAR(20)Associated request
event_atTIMESTAMPTZTool event timestamp
tool_nameVARCHAR(40)Tool invoked by the request
safety_event_log
ColumnTypeDescription
safety_event_idPKINTUnique safety event identifier
request_idVARCHAR(20)Associated request
event_atTIMESTAMPTZSafety event timestamp
decisionVARCHAR(20)Safety decision
Tablesrequest_logmodel_event_logtool_event_logsafety_event_log
Interviewer

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