The Airport With the Most Traffic - Problem

Given a Flights table containing flight traffic data between airports, find the airport(s) with the most total traffic.

The airport with the most traffic is defined as the airport that has the largest total number of flights that either departed from or arrived at that airport.

If there are multiple airports tied for the most traffic, return all of them.

  • Each row represents flights between two airports with the flight count
  • An airport's total traffic = sum of all flights departing from it + sum of all flights arriving at it
  • Return results in any order

Table Schema

Flights
Column Name Type Description
departure_airport PK int ID of the departure airport
arrival_airport PK int ID of the arrival airport
flights_count int Number of flights from departure to arrival airport
Primary Key: (departure_airport, arrival_airport)
Note: Each row represents flight traffic between two specific airports

Input & Output

Example 1 — Multiple Airports with Different Traffic
Input Table:
departure_airport arrival_airport flights_count
1 2 150
2 1 100
2 3 200
Output:
airport_id
2
💡 Note:

Airport traffic calculation: Airport 1 has 150 (departure) + 100 (arrival) = 250 total flights. Airport 2 has 100 + 200 (departures) + 150 (arrival) = 450 total flights. Airport 3 has 0 (departure) + 200 (arrival) = 200 total flights. Airport 2 has the highest traffic with 450 flights.

Example 2 — Tied for Maximum Traffic
Input Table:
departure_airport arrival_airport flights_count
1 2 100
2 1 100
1 3 50
3 2 50
Output:
airport_id
1
2
💡 Note:

Airport 1 has 100 + 50 (departures) + 100 (arrival) = 250 total flights. Airport 2 has 100 (departure) + 100 + 50 (arrivals) = 250 total flights. Airport 3 has 50 (departure) + 50 (arrival) = 100 total flights. Airports 1 and 2 are tied with 250 flights each, so both are returned.

Constraints

  • 1 ≤ departure_airport, arrival_airport ≤ 1000
  • 1 ≤ flights_count ≤ 10000
  • departure_airport ≠ arrival_airport

Visualization

Tap to expand
Airport Traffic Analysis OverviewInput: Flight Datadeparturearrivalcount121502110023200UNION ALL+ GROUP BYTraffic Totalsairporttraffic125024503200MAX FilterResult: Max Trafficairport_id2Airport 1: 150 + 100 = 250 flightsAirport 2: 300 + 150 = 450 flights ✓Airport 3: 0 + 200 = 200 flights
Understanding the Visualization
1
Input
Flights table with departure/arrival pairs
2
UNION ALL
Combine departure and arrival traffic
3
GROUP BY
Sum total traffic per airport
4
Filter MAX
Return airports with highest traffic
Key Takeaway
🎯 Key Insight: When counting bidirectional relationships, use UNION ALL to treat each entity from both perspectives, then aggregate the combined results
Asked in
Amazon 12 Microsoft 8 Google 6
18.5K Views
Medium Frequency
~12 min Avg. Time
895 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen