Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Coding Question Count

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

Your question is SQL Coding Question Count. Start with the requirements and the two 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

Bosch Building Technologies uses binary sensor readings from building automation systems to monitor equipment states. Write a PostgreSQL query that counts how often readings with values 0 and 1 occur for each building with at least one active device.

Requirements

  1. Join active devices to their sensor readings.
  2. Count readings equal to 0 and readings equal to 1 separately for each building.
  3. Include active buildings whose devices have no readings, returning zero counts where appropriate.
  4. Ignore NULL readings and values other than 0 or 1, and sort the results alphabetically by building name.

Schema

devices
ColumnTypeDescription
device_idPKINTEGERUnique device identifier
device_nameVARCHAR(100)Bosch Building Technologies device name
building_nameVARCHAR(100)Building where the device is installed
sensor_typeVARCHAR(50)Type of monitored sensor
is_activeBOOLEANWhether the device is currently active
sensor_readings
ColumnTypeDescription
reading_idPKINTEGERUnique reading identifier
device_idINTEGERDevice that produced the reading
reading_valueSMALLINTBinary or other recorded sensor value
Tablesdevicessensor_readings
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results