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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
SQL for Equipment Accountability
00:00
5 left

SQL for Equipment Accountability

MediumSQL · PostgreSQL

Problem

Write a SQL query to track equipment accountability by comparing government-furnished equipment checkouts, returns, and missing items across multiple exercise dates.

Use the supplied exercise, equipment, and transaction data. Treat an item as missing when it was checked out for an exercise but has not been returned for that exercise.

Output

  1. One row per exercise date, including dates with no transactions.
  2. Columns: exercise_date, exercise_name, checked_out_count, returned_count, missing_count, and missing_equipment_codes.
  3. Show missing equipment codes as a comma-separated, alphabetically ordered string, or an empty string when none are missing.
  4. Order by exercise_date ascending.

Output details

  • Preserve a separate output row for every exercise record, including records sharing the same exercise_date and exercise_name; do not deduplicate them.
  • Count every CHECKOUT and RETURN transaction, including multiple transactions for the same equipment and exercise.
  • Compute missing_count as the sum of each equipment's positive CHECKOUT minus RETURN difference, never allowing a negative contribution.
  • Include RETURN transactions even when their count exceeds the corresponding CHECKOUT count.
  • Separate missing equipment codes with comma-space, using ', ' rather than a comma alone.

Schema

exercises
ColumnTypeDescription
exercise_idPKINTUnique exercise identifier
exercise_nameVARCHAR(100)Exercise name
exercise_dateDATEDate of the exercise
notesTEXTOptional exercise notes
equipment
ColumnTypeDescription
equipment_idPKINTUnique equipment identifier
equipment_codeVARCHAR(30)Readable equipment accountability code
equipment_typeVARCHAR(50)Equipment category
serial_numberVARCHAR(50)Manufacturer serial number
equipment_transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
exercise_idINTExercise associated with the transaction
equipment_idINTEquipment involved in the transaction
event_typeVARCHAR(20)Transaction type, such as CHECKOUT or RETURN
event_dateDATEDate of the transaction
returned_byVARCHAR(100)Person recording a return
Tablesexercisesequipmentequipment_transactions
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

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.