-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path10.sql
More file actions
29 lines (28 loc) · 803 Bytes
/
10.sql
File metadata and controls
29 lines (28 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- Query 10: Top 5 airports with highest delayed flights in 2020
-- Identifies airports with the most delayed flights and calculates delay percentages
-- demonstrating fact/dimension joins and ranking analysis
WITH airport_delays AS (
SELECT
ap.airport,
COUNT(*) AS total_flights,
SUM(CASE WHEN f.arr_delay > 0 THEN 1 ELSE 0 END) AS delayed_flights,
ROUND(SUM(CASE WHEN f.arr_delay > 0 THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2) AS delay_percentage
FROM
flights f
JOIN
airports ap ON f.dest = ap.iata_code
WHERE
f.year = 2020
GROUP BY
ap.airport
)
SELECT
airport AS airport_name,
total_flights,
delayed_flights,
delay_percentage
FROM
airport_delays
ORDER BY
delayed_flights DESC
LIMIT 5;