0c690119f8db: Please Answer The Following Questions Using Airline DB Database
0c690119f8db: Please Answer The Following Questions Using Airline DB Database
0c690119f8db: Please Answer The Following Questions Using Airline DB Database
Write your answers(query) in the answer and submit it. To write the answer in the
assignment, please follow the below example in yellow
Example:
Questions: Extract all the columns of the flights table
Answer: SELECT * FROM flights
SELECT
book_ref,
TO_CHAR(book_date, 'YYYY-MM-DD') AS book_date,
total_amount
FROM
Bookings;
Answer:
SELECT
bp.ticket_no,
bp.boarding_no,
bp.seat_no,
t.passenger_id,
t.passenger_name
FROM
boarding_passes bp
JOIN
tickets t ON bp.ticket_no = t.ticket_no;
3. Write a query to find the seat number which is least allocated among all the seats?
Answer:
SELECT
seat_no
FROM
(
SELECT
seat_no,
COUNT(*) AS seat_count
FROM
boarding_passes
GROUP BY
seat_no
) AS seat_counts
WHERE
seat_counts.seat_count = (
SELECT
MIN(seat_count)
FROM
(
SELECT
seat_no,
COUNT(*) AS seat_count
FROM
boarding_passes
GROUP BY
seat_no
SQL Capstone Project
) AS min_counts
);
4. In the database, identify the month wise highest paying passenger name and
passenger id.
Expected output: Month_name(“mmm-yy” format), passenger_id, passenger_name and total
amount
Answer:
SELECT
month_name,
passenger_id,
passenger_name,
total_amount
FROM (
SELECT
TO_CHAR(b.book_date, 'MM-YY') AS month_name,
t.passenger_id,
t.passenger_name,
SUM(b.total_amount) AS total_amount,
RANK() OVER(PARTITION BY TO_CHAR(b.book_date, 'MM-YY') ORDER BY
SUM(b.total_amount) DESC) AS rank
FROM
Bookings b
JOIN
Tickets t ON t.book_ref = b.book_ref
GROUP BY
TO_CHAR(b.book_date, 'MM-YY'), t.passenger_id, t.passenger_name
) AS ranked_data
WHERE rank = 1;
5. In the database, identify the month wise least paying passenger name and
passenger id?
SQL Capstone Project
Answer:
SELECT
month_name,
passenger_id,
passenger_name,
total_amount
FROM (
SELECT
TO_CHAR(b.book_date, 'Mon-YY') AS month_name,
t.passenger_id,
t.passenger_name,
SUM(b.total_amount) AS total_amount,
RANK() OVER(PARTITION BY TO_CHAR(b.book_date, 'Mon-YY') ORDER BY
SUM(b.total_amount) ASC) AS rank
FROM
Bookings b
JOIN
Tickets t ON t.book_ref = b.book_ref
GROUP BY
TO_CHAR(b.book_date, 'Mon-YY'), t.passenger_id, t.passenger_name
) AS ranked_data
WHERE rank = 1;
6. Identify the travel details of non stop journeys or return journeys (having more than
1 flight).
Expected Output: Passenger_id, passenger_name, ticket_number and flight count.
Answer:
SELECT
t.passenger_id,
t.passenger_name,
t.ticket_no,
COUNT(tf.flight_id) AS flight_count
FROM
Tickets t
JOIN
Ticket_flights tf ON t.ticket_no = tf.ticket_no
GROUP BY
t.passenger_id,
SQL Capstone Project
t.passenger_name,
t.ticket_no
HAVING
COUNT(tf.flight_id) >= 1;
Answer:
SELECT
COUNT(*) AS ticket_count_no_pass
FROM
Tickets
WHERE
ticket_no NOT IN (SELECT DISTINCT ticket_no FROM boarding_passes);
Answer:
SELECT
flight_no AS flight_number,
departure_airport,
arrival_airport,
aircraft_code,
EXTRACT(EPOCH FROM (scheduled_arrival - scheduled_departure)) / 60 AS
duration
FROM
Flights
ORDER BY
duration DESC
LIMIT 1;
9. Identify details of all the morning flights (morning means between 6AM to 11 AM,
using flights table)?
Expected output: flight_id, flight_number, scheduled_departure, scheduled_arrival and timings.
SQL Capstone Project
Answer:
SELECT
flight_id,
flight_no AS flight_number,
scheduled_departure,
scheduled_arrival,
'Morning' AS timings
FROM
Flights
WHERE
EXTRACT(HOUR FROM scheduled_departure) >= 6
AND EXTRACT(HOUR FROM scheduled_departure) < 12;
10. Identify the earliest morning flight available from every airport.
Expected output: flight_id, flight_number, scheduled_departure, scheduled_arrival, departure
airport and timings.
Answer:
WITH EarlyMorningFlights AS (
SELECT
flight_id,
flight_no AS flight_number,
scheduled_departure,
scheduled_arrival,
departure_airport,
ROW_NUMBER() OVER (PARTITION BY departure_airport ORDER BY
scheduled_departure) AS rn
FROM
Flights
WHERE
EXTRACT(HOUR FROM scheduled_departure) >= 6
AND EXTRACT(HOUR FROM scheduled_departure) < 12
)
SELECT
flight_id,
flight_number,
scheduled_departure,
scheduled_arrival,
departure_airport,
'Morning' AS timings
FROM
EarlyMorningFlights
WHERE
SQL Capstone Project
rn = 1;
Answer:
SELECT
airport_code
FROM
Airports
WHERE
timezone = 'Europe/Moscow';
12. Write a query to get the count of seats in various fare condition for every aircraft code?
Expected Outputs: Aircraft_code, fare_conditions ,seat count
Answer:
SELECT
aircraft_code,
fare_conditions,
COUNT(*) AS seat_count
FROM
Seats
GROUP BY
aircraft_code,
fare_conditions;
13. How many aircrafts codes have at least one Business class seats?
Expected Output : Count of aircraft codes
Answer:
SELECT
COUNT(DISTINCT aircraft_code) AS aircraft_with_business_class
SQL Capstone Project
FROM
Seats
WHERE
fare_conditions = 'Business';
14. Find out the name of the airport having maximum number of departure flight
Expected Output : Airport_name
Answer:
SELECT
a.airport_name
FROM
Airports a
JOIN
Flights f ON a.airport_code = f.departure_airport
GROUP BY
a.airport_name
ORDER BY
COUNT(*) DESC
LIMIT 1;
15. Find out the name of the airport having least number of scheduled departure flights
Expected Output : Airport_name
Answer:
SELECT
a.airport_name
FROM
Airports a
JOIN
Flights f ON a.airport_code = f.departure_airport
GROUP BY
a.airport_name
ORDER BY
COUNT(*) ASC
LIMIT 1;
16. How many flights from ‘DME’ airport don’t have actual departure?
Expected Output : Flight Count
SQL Capstone Project
Answer:
SELECT
COUNT(*) AS flight_count
FROM
Flights
WHERE
departure_airport = 'DME'
AND actual_departure IS NULL;
Answer:
SELECT
flight_no AS Flight_Number,
aircraft_code,
'3000-6000' AS ranges
FROM
Flights
WHERE
flight_id BETWEEN 3000 AND 6000;
18. Write a query to get the count of flights flying between URS and KUF?
Expected Output : Flight_count
Answer:
SELECT
COUNT(*) AS Flight_count
FROM
Flights
WHERE
departure_airport = 'URS'
AND arrival_airport = 'KUF';
19. Write a query to get the count of flights flying from either from NOZ or KRR?
Expected Output : Flight count
Answer:
SQL Capstone Project
SELECT
COUNT(*) AS Flight_count
FROM
Flights
WHERE
departure_airport IN ('NOZ', 'KRR');
20. Write a query to get the count of flights flying from KZN,DME,NBC,NJC,GDX,SGC,VKO,ROV
Expected Output : Departure airport ,count of flights flying from these airports.
Answer:
SELECT
departure_airport,
COUNT(*) AS count
FROM
Flights
WHERE
departure_airport IN ('KZN', 'DME', 'NBC', 'NJC', 'GDX', 'SGC', 'VKO', 'ROV')
GROUP BY
departure_airport;
21. Write a query to extract flight details having range between 3000 and 6000 and flying from
DME
Expected Output :Flight_no,aircraft_code,range,departure_airport
Answer:
SELECT
flight_no,
aircraft_code,
'3000-6000' AS Range,
departure_airport
FROM
Flights
WHERE
flight_no BETWEEN 3000 AND 6000
SQL Capstone Project
22. Find the list of flight ids which are using aircrafts from “Airbus” company and got cancelled
or delayed
Expected Output : Flight_id,aircraft_model
Answer:
SELECT
f.flight_id,
a.model AS aircraft_model
FROM
Flights f
JOIN
Aircrafts a ON f.aircraft_code = a.aircraft_code
WHERE
a.model LIKE '%Airbus%'
AND (f.status = 'Delayed' OR f.status = 'Cancelled');
23. Find the list of flight ids which are using aircrafts from “Boeing” company and got cancelled
or delayed
Expected Output : Flight_id,aircraft_model
Answer:
SELECT
f.flight_id,
a.model AS aircraft_model
FROM
Flights f
JOIN
Aircrafts a ON f.aircraft_code = a.aircraft_code
WHERE
a.model LIKE '%Boeing%'
AND (f.status = 'Delayed' OR f.status = 'Cancelled');
Answer:
SQL Capstone Project
SELECT
a.airport_name,
COUNT(*) AS flights_count
FROM
Airports a
JOIN
Flights f ON a.airport_code = f.arrival_airport
WHERE
f.status = 'Cancelled'
GROUP BY
a.airport_name
ORDER BY
flights_count DESC
LIMIT 1;
Answer:
SELECT
f.flight_id,
a.model AS aircraft_model
FROM
Flights f
JOIN
Aircrafts a ON f.aircraft_code = a.aircraft_code
WHERE
a.model LIKE '%Airbus%'
Answer:
WITH LastFlights AS (
SELECT
flight_id,
flight_no AS flight_number,
scheduled_departure,
departure_airport,
ROW_NUMBER() OVER (PARTITION BY departure_airport ORDER BY
scheduled_departure DESC) AS rn
FROM
SQL Capstone Project
Flights
)
SELECT
flight_id,
flight_number,
scheduled_departure,
departure_airport
FROM
LastFlights
WHERE
rn = 1;
27. Identify list of customers who will get the refund due to cancellation of the flights and how
much amount they will get?
Expected Output : Passenger_name,total_refund.
Answer:
SELECT
t.passenger_name,
SUM(b.total_amount) AS Total_refund
FROM
Tickets t
JOIN
Bookings b ON t.book_ref = b.book_ref
JOIN
Ticket_flights tf ON tf.ticket_no = t.ticket_no
JOIN
Flights f ON f.flight_id = tf.flight_id
WHERE
f.status = 'Cancelled'
GROUP BY
t.passenger_name;
28. Identify date wise first cancelled flight id flying for every airport?
Expected Output : Flight_id,flight_number,schedule_departure,departure_airport
Answer:
WITH FirstCancelledFlights AS (
SELECT
flight_id,
SQL Capstone Project
flight_no AS flight_number,
scheduled_departure,
departure_airport,
ROW_NUMBER() OVER (PARTITION BY departure_airport ORDER BY
scheduled_departure) AS rn
FROM
Flights
WHERE
status = 'Cancelled'
)
SELECT
flight_id,
flight_number,
scheduled_departure,
departure_airport
FROM
FirstCancelledFlights
WHERE
rn = 1;
Answer:
SELECT
f.flight_id
FROM
Flights f
JOIN
Aircrafts a ON f.aircraft_code = a.aircraft_code
WHERE
a.model LIKE '%Airbus%'
AND f.status = 'Cancelled';
Answer:
SELECT
f.flight_no,
a.range
FROM
Flights f
JOIN
Aircrafts a ON f.aircraft_code = a.aircraft_code
WHERE
a.range = (
SELECT
MAX(range)
FROM
Aircrafts
);