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
SQL Joins and Time-Window Aggregations
00:00
5 left

SQL Joins and Time-Window Aggregations

MediumSQL · PostgreSQL

Problem

Write a SQL query to join multiple tables and aggregate performance metrics over a specific time window.

Use the provided site, asset, and performance record tables. Analyze records from January 1, 2025 through March 31, 2025, inclusive. Include sites without qualifying performance records.

Output

  1. One row per site.
  2. Return site_id, site_name, asset_count, avg_availability_pct, total_production_units, and avg_energy_kwh.
  3. Count all assets assigned to each site, but calculate metrics only from records in the time window.
  4. Preserve NULL for averages with no qualifying values, use zero for total production with no qualifying records, and order by site_id ascending.

Schema

sites
ColumnTypeDescription
site_idPKINTUnique site identifier
site_nameVARCHAR(100)Site name
regionVARCHAR(50)Geographic region
assets
ColumnTypeDescription
asset_idPKINTUnique asset identifier
site_idINTSite assigned to the asset
asset_nameVARCHAR(100)Asset name
asset_typeVARCHAR(50)Asset category
performance_records
ColumnTypeDescription
record_idPKINTUnique performance record identifier
asset_idINTAsset measured by the record
measured_atDATEDate of the performance measurement
availability_pctDECIMAL(5,2)Asset availability percentage
production_unitsDECIMAL(12,2)Units produced during the measurement period
energy_kwhDECIMAL(12,2)Energy consumed in kilowatt-hours
Tablessitesassetsperformance_records
Interviewer

Your question is SQL Joins and Time-Window Aggregations. 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.