Travel: Multi-City Lag

10pts
Expedia, United Airlines

Problem Statement

Hidden-city ticketing is a travel hack where a passenger books a flight from City A to City C with a layover in City B because it is cheaper than a direct flight from A to B. The passenger then exits at the layover city, skipping the final leg.

Write a query to identify these passengers. A Hidden-City flyer is defined as:

  1. A user who has a confirmed booking for a multi-leg journey (e.g., Leg 1: A to B, Leg 2: B to C).
  2. The flight_logs show that the passenger successfully completed all legs except the very last one.
  3. The flight_logs show no record of the passenger flying the final leg of that booking.

Rules:

  • Both legs must belong to the same booking_id.
  • Leg 2 must start from the destination of Leg 1.
  • Output columns: user_id, booking_id, skipped_destination.
  • skipped_destination is the final city of the booked itinerary that was not flown.
  • Result must be sorted by booking_id ascending.
Tests your understanding of
Self Joins, Filtering and Conditional Logic

Input Tables

bookings
booking_id(INTEGER)user_id(INTEGER)leg_number(INTEGER)origin(VARCHAR)destination(VARCHAR)
1015011SFOORD
1015012ORDLGA
1025021LAXDFW
1025022DFWMIA
1035031SEADEN
1035032DENJFK
1045041BOSATL
1055051JFKDXB
1055052DXBMLE
1065061LHRCDG
1065062CDGSIN
flight_logs
log_id(INTEGER)booking_id(INTEGER)leg_number(INTEGER)status(VARCHAR)
11011Flown
21012Flown
31021Flown
41031Flown
51041Flown
61051Flown
71061Flown

Expected Output

user_id(INTEGER)booking_id(INTEGER)skipped_destination(VARCHAR)
502102MIA
503103JFK
505105MLE
506106SIN

Tags

HardSelf JoinsFilteringConditional Logic
30-40 min
29%

Hints