Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Second Value and OOP Concepts

HardSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Second Value and OOP Concepts. Start with the requirements and the four tables on the right.

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.

Problem

AVEVA Industrial Intelligence analysts need to compare the first two valid temperature readings for active UK assets. Write a PostgreSQL query that returns the second chronologically recorded reading for each eligible asset.

Requirements

  1. Consider only active assets at sites in the UK.
  2. Include only non-null temperature readings with quality_code = 'GOOD'.
  3. Use recorded_at, then reading_id as a deterministic tie-breaker, and return assets with at least two valid readings.
  4. Exclude assets whose latest maintenance event has status OPEN.
  5. Return the site, asset, first reading, second reading, and change from the first to the second reading.

Schema

aveva_sites
ColumnTypeDescription
site_idPKINTIndustrial site identifier
site_nameVARCHAR(100)Site name
country_codeVARCHAR(2)ISO-style country code
aveva_assets
ColumnTypeDescription
asset_idPKINTAsset identifier
asset_nameVARCHAR(100)Asset name
site_idINTOwning site identifier
asset_typeVARCHAR(40)Industrial asset type
commissioned_dateDATEDate the asset entered service
is_activeBOOLEANWhether the asset is operational
asset_readings
ColumnTypeDescription
reading_idPKINTReading identifier
asset_idINTMeasured asset identifier
reading_typeVARCHAR(40)Measurement type
recorded_atTIMESTAMPTZMeasurement timestamp
reading_valueNUMERIC(10,2)Measured value
quality_codeVARCHAR(20)Measurement quality status
maintenance_events
ColumnTypeDescription
maintenance_event_idPKINTMaintenance event identifier
asset_idINTMaintained asset identifier
event_dateDATEMaintenance event date
maintenance_statusVARCHAR(20)Maintenance state
Tablesaveva_sitesaveva_assetsasset_readingsmaintenance_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results