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

Optimize SQL for Large Datasets

HardSQL · PostgreSQL

Problem

Business Context

Microsoft Teams analytics needs a weekly engagement report across active workspaces. The source tables contain millions of messages and reactions, so the query must reduce data before expensive joins and window calculations.

Task

Write an optimized PostgreSQL query for messages sent from 2025-01-01 through 2025-02-28. Return weekly metrics for active Teams workspaces and retain weeks with at least two qualifying messages.

Requirements

  1. Filter deleted messages, non-standard messages, inactive workspaces, and inactive authors before aggregation.
  2. Pre-aggregate reactions by message to avoid multiplying message rows during joins.
  3. Calculate message count, distinct active authors, total reactions, and reactions per message.
  4. Use LAG to show the previous observed week's message count and percentage change.
  5. Return only weeks with at least two messages, ordered by workspace and week.

Schema

teams
ColumnTypeDescription
team_idPKINTMicrosoft Teams workspace identifier
team_nameVARCHAR(100)Workspace name
statusVARCHAR(20)Workspace lifecycle status
users
ColumnTypeDescription
user_idPKINTMicrosoft Entra user identifier
display_nameVARCHAR(100)User display name
account_statusVARCHAR(20)User account status
channel_messages
ColumnTypeDescription
message_idPKBIGINTChannel message identifier
team_idINTReferences teams.team_id
author_idINTReferences users.user_id
sent_atTIMESTAMPMessage creation timestamp
is_deletedBOOLEANWhether the message was deleted
message_typeVARCHAR(30)Message classification
message_reactions
ColumnTypeDescription
reaction_idPKBIGINTReaction identifier
message_idBIGINTReferences channel_messages.message_id
user_idINTUser who added the reaction
reaction_typeVARCHAR(30)Reaction category
Tablesteamsuserschannel_messagesmessage_reactions
Interviewer

Your question is Optimize SQL for Large Datasets. Start with the requirements and the four 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.