Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Average Transit Time Calculation
00:00
5 left

Average Transit Time Calculation

MediumSQL · PostgreSQL

Problem

Given a table of historical port arrivals, how would you calculate the average transit time between two specific ports over the last quarter at Mediterranean Shipping?

Use the historical_port_arrivals table and calculate transit time from departure_at to arrival_at. Include only completed trips between Valencia and Genoa during the previous calendar quarter.

Output

  1. Return one row with origin_port, destination_port, and average_transit_hours.
  2. Round the average to two decimal places.
  3. Exclude incomplete, unrelated, and out-of-period trips.

Schema

historical_port_arrivals
ColumnTypeDescription
arrival_idPKINTUnique historical arrival record identifier
vessel_nameVARCHAR(100)Name of the vessel completing the voyage
origin_portVARCHAR(100)Port from which the voyage departed
destination_portVARCHAR(100)Port at which the vessel arrived
departure_atTIMESTAMPDeparture timestamp from the origin port
arrival_atTIMESTAMPArrival timestamp at the destination port
Tableshistorical_port_arrivals
Interviewer

Your question is Average Transit Time Calculation. Start with the requirements and the one table 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.