Flight Routes - 2
0:00:00
Delta Airlines is analyzing how passengers travel across its domestic network. The analytics team wants to understand which routes are most commonly flown out of each airport, and how long these popular journeys usually take. You’re working as a data analyst at Delta Airlines, and you’re provided with a table flights where each row represents one scheduled flight.
Write a SQL query to return the top 5 most frequent flight routes for each origin, ordered first by frequency descending, and then (for ties) by average journey duration ascending.
Notes:
- A route is defined as a unique
(origin, destination)pair. - Assume there is no more then 5 routes with the same frequency and average filght time.
Schema:
Input:
flights
| Column | Type |
|---|---|
| flight_id | INTEGER |
| origin | VARCHAR |
| destination | VARCHAR |
| departure_time | DATETIME |
| arrival_time | DATETIME |
Output:
| Column | Type |
|---|---|
| origin | VARCHAR |
| destination | VARCHAR |
| route_frequency | INTEGER |
| avg_duration_minutes | DATETIME |
Example:
Input Data
flights
| flight_id | origin | destination | departure_time | arrival_time |
|---|---|---|---|---|
| 1 | JFK | LAX | 2025-11-19 08:00 | 2025-11-19 11:30 |
| 2 | JFK | LAX | 2025-11-19 14:00 | 2025-11-19 17:40 |
| 3 | JFK | ORD | 2025-11-19 09:00 | 2025-11-19 10:45 |
| 4 | JFK | ORD | 2025-11-20 09:00 | 2025-11-20 10:42 |
| 5 | JFK | MIA | 2025-11-19 12:00 | 2025-11-19 15:00 |
| 6 | JFK | MIA | 2025-11-20 12:00 | 2025-11-20 14:55 |
| 7 | LAX | SFO | 2025-11-19 07:00 | 2025-11-19 08:25 |
| 8 | LAX | SFO | 2025-11-19 16:00 | 2025-11-19 17:20 |
| 9 | LAX | SEA | 2025-11-19 06:00 | 2025-11-19 08:40 |
| 10 | LAX | SEA | 2025-11-20 06:00 | 2025-11-20 08:35 |
Output
| origin | destination | route_frequency | avg_duration_minutes |
|---|---|---|---|
| JFK | LAX | 2 | 210 |
| JFK | ORD | 2 | 103 |
| JFK | MIA | 2 | 172 |
| LAX | SFO | 2 | 82 |
| LAX | SEA | 2 | 157 |
Explanation:
From each origin (JFK, LAX), the query returns the top 5 most frequent destination routes. Since each origin has only two routes in this dataset, they are all included. For routes with the same frequency, sorting is done by increasing average duration.
.
.
.
.
Comments