Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Compute Running Totals in SQL

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

Your question is Compute Running Totals in SQL. 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

Caterpillar operations analysts need a chronological view of completed equipment shipments from active manufacturing plants. Write a PostgreSQL query that calculates daily shipped units and a running total for each active plant.

Requirements

  1. Join shipment_events to plants and include only active plants.
  2. Include only shipments with status completed within January 2025.
  3. Aggregate shipment units by plant and shipment date, treating NULL unit values as zero.
  4. Use a window function to calculate the cumulative shipment total for each plant, ordered by shipment date. Return rows ordered by plant name and date.

Schema

plants
ColumnTypeDescription
plant_idPKINTEGERUnique manufacturing plant identifier
plant_nameVARCHAR(100)Caterpillar manufacturing plant name
regionVARCHAR(50)Operating region
is_activeBOOLEANWhether the plant is active
shipment_events
ColumnTypeDescription
shipment_idPKINTEGERUnique shipment event identifier
plant_idINTEGERManufacturing plant associated with the event
shipment_dateDATEShipment activity date
units_shippedINTEGERNumber of units shipped
statusVARCHAR(20)Shipment processing status
Tablesplantsshipment_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results