Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Optimizing Large Join Queries
00:00
5 left

Optimizing Large Join Queries

HardSQL · PostgreSQL

Problem

How do you optimize complex queries that involve joining multi-million row tables?

Provide a PostgreSQL query for the report below and explain the performance decisions you would validate with EXPLAIN (ANALYZE, BUFFERS). The result should preserve matters with no matching events.

Output

  1. One row per qualifying matter, with matter_id, organization_name, qualifying_event_count, and latest_activity_at
  2. Include open or paused matters for enterprise organizations created before 2025-04-01
  3. Count only review and filing events during Q1 2025, while latest_activity_at considers all Q1 events
  4. Sort by qualifying event count descending, latest activity descending with nulls last, then matter ID ascending

Schema

organizations
ColumnTypeDescription
organization_idPKINTUnique organization identifier
organization_nameVARCHAR(120)Organization display name
regionVARCHAR(40)Primary operating region
segmentVARCHAR(30)Organization commercial segment
matters
ColumnTypeDescription
matter_idPKINTUnique matter identifier
organization_idINTOwning organization identifier
matter_titleVARCHAR(160)Matter title
statusVARCHAR(20)Current matter status
created_atDATEMatter creation date
matter_events
ColumnTypeDescription
event_idPKINTUnique event identifier
matter_idINTRelated matter identifier
event_typeVARCHAR(30)Event classification
event_atTIMESTAMPEvent timestamp
Tablesorganizationsmattersmatter_events
Interviewer

Your question is Optimizing Large Join Queries. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.