The Airport With the Most Traffic - Problem

๐Ÿ›ซ The Airport With the Most Traffic

You're working as a data analyst for a major aviation authority, and you need to identify which airports handle the most flight traffic. Given a database table containing flight route information, your task is to find the airport(s) with the highest total traffic.

The Problem: Calculate the total number of flights for each airport by counting both departing and arriving flights. An airport's traffic score is the sum of all flights that either departed from or arrived at that specific airport.

Input: A Flights table with three columns:

  • departure_airport (int): ID of the departure airport
  • arrival_airport (int): ID of the arrival airport
  • flights_count (int): Number of flights on this route

Output: Return the airport ID(s) with the maximum total traffic. If multiple airports tie for the highest traffic, return all of them.

Key Insight: Each flight route contributes to the traffic count of two airports - the departure airport and the arrival airport!

Input & Output

example_1.sql โ€” Basic Traffic Analysis
$ Input: Flights table: | departure_airport | arrival_airport | flights_count | |-------------------|-----------------|---------------| | 1 | 2 | 15 | | 2 | 1 | 10 | | 2 | 3 | 25 | | 3 | 2 | 5 |
โ€บ Output: | airport_id | |------------| | 2 |
๐Ÿ’ก Note: Airport 1: 15 (departure) + 10 (arrival) = 25 total flights. Airport 2: 10 + 25 (departures) + 15 + 5 (arrivals) = 55 total flights. Airport 3: 25 (departure) + 5 (arrival) = 30 total flights. Airport 2 has the highest traffic with 55 flights.
example_2.sql โ€” Tied Airports
$ Input: Flights table: | departure_airport | arrival_airport | flights_count | |-------------------|-----------------|---------------| | 1 | 3 | 20 | | 2 | 3 | 20 | | 3 | 1 | 10 | | 3 | 2 | 10 |
โ€บ Output: | airport_id | |------------| | 1 | | 2 | | 3 |
๐Ÿ’ก Note: Airport 1: 20 (departure) + 10 (arrival) = 30 total flights. Airport 2: 20 (departure) + 10 (arrival) = 30 total flights. Airport 3: 10 + 10 (departures) + 20 + 20 (arrivals) = 60 total flights. Wait, let me recalculate: All airports have 30 flights each, so all three are returned as they tie for maximum traffic.
example_3.sql โ€” Single Route
$ Input: Flights table: | departure_airport | arrival_airport | flights_count | |-------------------|-----------------|---------------| | 5 | 7 | 100 |
โ€บ Output: | airport_id | |------------| | 5 | | 7 |
๐Ÿ’ก Note: Only one flight route exists. Airport 5 has 100 departure flights and Airport 7 has 100 arrival flights. Both airports tie with 100 total flights each, so both are returned.

Constraints

  • 1 โ‰ค Number of rows in Flights โ‰ค 104
  • 1 โ‰ค departure_airport, arrival_airport โ‰ค 103
  • 1 โ‰ค flights_count โ‰ค 104
  • departure_airport โ‰  arrival_airport (no self-loops)
  • The combination (departure_airport, arrival_airport) is unique for each row

Visualization

Tap to expand
๐Ÿ›ซ Airport Traffic Flow AnalysisA1LAXA2JFKA3ORD25 flights15 flights30 flightsTraffic CalculationA1 (LAX): 25 + 15 = 40A2 (JFK): 15 + 25 + 30 = 70A3 (ORD): 30 = 30Winner: A2 with 70 flights!SQL Logic1. UNION ALL departure & arrival2. GROUP BY airport_id3. SUM(flights_count)4. Find MAX and filterโšก O(n) time complexityKey InsightEach flight contributes toBOTH airports:โœˆ๏ธ Departure airport๐Ÿ›ฌ Arrival airport๐Ÿ’ก UNION ALL creates a unified stream for efficient single-pass processing๐ŸŽฏ Result: Airport A2 (JFK) has the most traffic!
Understanding the Visualization
1
Identify Traffic Sources
Each flight record represents traffic flowing between two airports
2
Split Traffic Streams
Use UNION ALL to create separate records for departure and arrival traffic
3
Aggregate by Airport
GROUP BY airport_id to sum all traffic for each airport
4
Find Peak Traffic
Identify airport(s) with maximum total traffic
Key Takeaway
๐ŸŽฏ Key Insight: Using UNION ALL to combine departure and arrival streams allows for efficient single-pass aggregation, achieving optimal O(n) performance while correctly counting each flight's contribution to both airports involved.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
42.3K Views
High Frequency
~15 min Avg. Time
1.8K 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