Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Self-Join for Telemetry Sequences
00:00
5 left

Self-Join for Telemetry Sequences

MediumSQL · PostgreSQL

Problem

Write a query using a self-join to track sequential events in a vehicle telemetry log. Return each event with the next chronological event for the same vehicle and the elapsed time between them. An event is sequential only when no other event for that vehicle occurs between the two events.

Output

  1. One row per event that has a following event.
  2. Columns: vehicle_id, current_event_id, current_event_type, current_event_time, next_event_id, next_event_type, next_event_time, and seconds_to_next.
  3. Order by vehicle_id, current_event_time, and current_event_id ascending.

Schema

vehicle_telemetry
ColumnTypeDescription
event_idPKINTUnique telemetry event identifier
vehicle_idVARCHAR(20)Vehicle identifier associated with the event
event_timeTIMESTAMPTimestamp when the telemetry event occurred
event_typeVARCHAR(40)Telemetry event classification
Tablesvehicle_telemetry
Interviewer

Your question is Self-Join for Telemetry Sequences. 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.