Sei sulla pagina 1di 20

Airlines

Write a query to display the average monthly ticket cost for each flight in ABC Airlines. The
query should display the Flight_Id,From_location,To_Location,Month Name as
“Month_Name” and average price as “Average_Price”
Display the records sorted in ascending order based on flight id and then by Month Name.
SELECT f.flight_id,
f.from_location,
f.to_location,
fp.Month_Name,
fp.Average_Price
FROM air_flight f,
(
SELECT flight_id,
MONTHNAME(flight_departure_date) AS Month_Name,
AVG(price) as Average_Price
FROM air_flight_details
GROUP BY flight_id,
MONTHNAME(flight_departure_date)
) fp
WHERE f.flight_id = fp.flight_id
andf.airline_name = 'ABC AIRLINES'
order by f.flight_id, fp.Month_Name;

Write a query to display the customer(s) who has/have booked least number of tickets in
ABC Airlines. The Query should display profile_id, customer’s first_name, Address and
Number of tickets booked as “No_of_Tickets”
Display the records sorted in ascending order based on customer's first name.
SELECT cus.profile_id,
cus.first_name,
cus.address,
mintkt.No_of_Tickets
FROM air_passenger_profilecus,
(SELECT MIN(s.Tot_No_of_Tickets) AS No_of_Tickets
FROM (SELECT profile_id,
COUNT(ticket_id) AS Tot_No_of_Tickets
FROM air_ticket_info
GROUP BY profile_id
)s
) mintkt,
( SELECTprofile_id,
COUNT(ticket_id) AS Tot_No_of_Tickets
FROM air_ticket_info
GROUP BY profile_id
) tottkt
WHERE mintkt.No_of_Tickets = tottkt.Tot_No_of_Tickets
AND cus.profile_id = tottkt.profile_id
order by cus.first_name;
Write a query to display the number of flight services between locations in a month. The
Query should display From_Location, To_Location, Month as “Month_Name” and number
of flight services as “No_of_Services”.
Hint: The Number of Services can be calculated from the number of scheduled departure
dates of a flight.
The records should be displayed in ascending order based on From_Location and then by
To_Location and then by month name.
SELECT f.from_location,
f.to_location,
s.Month_Name,
SUM(s.No_of_Services) AS No_of_Services
FROM air_flight f JOIN
(SELECT flight_id,
MONTHNAME(flight_departure_date) AS Month_Name,
COUNT(flight_departure_date) AS No_of_Services
FROM air_flight_details
GROUP BY flight_id,
MONTHNAME(flight_departure_date)
)s
ON s.flight_id =f.flight_id
GROUP BY f.from_location,
f.to_Location,
s.Month_Name
order by f.from_location,
f.to_Location, s.Month_Name;

Write a query to display the customer(s) who has/have booked maximum number of tickets in
ABC Airlines. The Query should display profile_id, customer’s first_name, Address and
Number of tickets booked as “No_of_Tickets”
Display the records in ascending order based on customer's first name.
SELECT cus.profile_id,
cus.first_name,
cus.address,
maxtkt.No_of_Tickets as No_of_Tickets
FROM air_passenger_profilecus,
(SELECT MAX(s.Tot_No_of_Tickets) AS No_of_Tickets
FROM (SELECT profile_id,
COUNT(ticket_id) AS Tot_No_of_Tickets
FROM air_ticket_info
GROUP BY profile_id
)s
) maxtkt,
( SELECTprofile_id,
COUNT(ticket_id) AS Tot_No_of_Tickets
FROM air_ticket_info
GROUP BY profile_id
) tottkt
WHERE maxtkt.No_of_Tickets = tottkt.Tot_No_of_Tickets
AND cus.profile_id = tottkt.profile_id
order by cus.first_name;
Write a query to display the number of tickets booked from Chennai to Hyderabad. The
Query should display passenger profile_id,first_name,last_name, Flight_Id , Departure_Date
and number of tickets booked as “No_of_Tickets”.

Display the records sorted in ascending order based on profile id and then by flight id and
then by departure date.
SELECT cus.profile_id,
cus.first_name,
cus.last_name,
tkt.flight_id,
tkt.flight_departure_date,
tkt.No_of_Tickets
FROM air_passenger_profilecus JOIN
(
SELECT profile_id, FLIGHT_ID,FLIGHT_DEPARTURE_DATE,COUNT(ticket_id) AS
No_of_Tickets
FROM air_ticket_info
WHERE flight_id IN (SELECT flight_id
FROM air_flight
WHERE from_location = 'Chennai'
AND to_location = 'Hyderabad'
)
GROUP BY profile_id,flight_id,flight_departure_date
)tkt
ON cus.profile_id = tkt.profile_id
order by cus.profile_id, tkt.flight_id, tkt.flight_departure_date;

Write a query to display flight id,from location, to location and ticket price of flights whose
departure is in the month of april.
Display the records sorted in ascending order based on flight id and then by from location.

SELECT fd.flight_id,af.FROM_LOCATION,af.TO_LOCATION, fd.price FROM


air_flight_detailsfd join air_flightaf on af.FLIGHT_ID=fd.FLIGHT_ID where
substring(fd.flight_departure_date,6,2)='04' order by fd.flight_id, af.FROM_LOCATION;

Write a query to display the average cost of the tickets in each flight on all scheduled dates.
The query should display flight_id, from_location, to_location and Average price as “Price”.
Display the records sorted in ascending order based on flight id and then by from_location
and then by to_location.

SELECT f.flight_id,
f.from_location,
f.to_location,
AVG(fd.Price) AS Price
FROM air_flight f JOIN
air_flight_detailsfd
ON f.flight_id = fd.flight_id
GROUP BY f.flight_id order by f.flight_id, f.from_location, f.to_location;

Write a query to display the customers who have booked tickets from Chennai to Hyderabad.
The query should display profile_id, customer_name (combine first_name&last_name with
comma in b/w), address of the customer.
Give an alias to the name as customer_name.
Hint: Query should fetch unique customers irrespective of multiple tickets booked.
Display the records sorted in ascending order based on profile id.

SELECT DISTINCT c.profile_id,


CONCAT(c.first_name, ',',c.last_name) AS customer_name,
c.address
FROM air_passenger_profile c
JOIN air_ticket_info t
ON c.profile_id = t.profile_id
JOIN air_flight f
ON f.flight_id = t.flight_id
AND f.from_location = 'Chennai'
AND f.to_location = 'Hyderabad'
order by c.profile_id;

Write a query to display profile id of the passenger(s) who has/have booked maximum
number of tickets.
In case of multiple records, display the records sorted in ascending order based on profile id.

SELECT profile_id FROM air_ticket_info group by profile_id having


count(profile_id)>=all(select count(profile_id) from air_ticket_info group by profile_id)
order by profile_id;

Write a query to display the total number of tickets as “No_of_Tickets” booked in each flight
in ABC Airlines. The Query should display the flight_id, from_location, to_location and the
number of tickets.
Display only the flights in which atleast 1 ticket is booked.
Display the records sorted in ascending order based on flight id.

SELECT f.flight_id,
f.from_location,
f.to_location,
COUNT(t.ticket_id) AS No_of_Tickets
FROM air_ticket_infot JOIN
air_flight f
ON f.flight_id = t.flight_id
where AIRLINE_NAME = 'ABC AIRLINES'
GROUP by f.flight_id
ORDER by f.flight_id;

Write a query to display the no of services offered by each flight and the total price of the
services. The Query should display flight_id, number of services as “No_of_Services” and
the cost as “Total_Price” in the same order.
Order the result by Total Price in descending order and then by flight_id in descending order.
Hint:The number of services can be calculated from the number of scheduled departure dates
of the flight

SELECT flight_id,
COUNT(flight_departure_date) AS No_of_Services,
SUM(price) AS Total_Price
FROM air_flight_details
GROUP BY flight_id order by total_price DESC, flight_id DESC;

Write a query to display the number of passengers who have travelled in each flight in each
scheduled date. The Query should display flight_id, flight_departure_date and the number of
passengers as “No_of_Passengers” in the same order.
Display the records sorted in ascending order based on flight id and then by flight departure
date.
SELECT flight_id,
flight_departure_date,
COUNT(ticket_id) AS No_of_Passengers
FROM air_ticket_info
GROUP BY flight_id,
flight_departure_date
ORDER BY flight_id, flight_departure_date;

Write a query to display profile id of passenger(s) who booked minimum number of tickets.
In case of multiple records, display the records sorted in ascending order based on profile id.

SELECT profile_id FROM air_ticket_info group by profile_id having


count(profile_id)<=all(select count(profile_id) from air_ticket_info group by profile_id)
order by profile_id;

Write a query to display unique passenger profile id,firstname,mobile number and email
address of passengers who booked ticket to travel from HYDERABAD to CHENNAI.
Display the records sorted in ascending order based on profile id.

SELECT distinct ti.PROFILE_ID,pi.first_name,pi.mobile_number, pi.email_id FROM


air_ticket_infoti join air_passenger_profile pi on pi.profile_id=ti.profile_id where flight_id in
(SELECT FLIGHT_ID FROM air_flight where FROM_LOCATION ='HYDERABAD' and
to_location ='CHENNAI') order by ti.profile_id
Write a query to intimate the passengers who are boarding Chennai to Hyderabad Flight on
6th May 2013 stating the delay of 1hr in the departure time. The Query should display the
passenger’s profile_id, first_name,last_name, flight_id, flight_departure_date, actual
departure time , actual arrival time , delayed departure time as "Delayed_Departure_Time",
delayed arrival time as "Delayed_Arrival_Time" Hint: Distinct Profile ID should be
displayed irrespective of multiple tickets booked by the same profile.
Display the records sorted in ascending order based on passenger's profile id.

SELECT DISTINCT p.profile_id,


p.first_name,
p.last_name,
t.flight_id,
t.flight_departure_date,
f.departure_time,
f.arrival_time,
ADDTIME(f.departure_time,'01:00:00') AS Delayed_Departure_Time,
ADDTIME(f.arrival_time,'01:00:00') AS Delayed_Arrival_Time
FROM air_passenger_profile p
JOIN air_ticket_info t
ON p.profile_id = t.profile_id
AND t.flight_departure_date = '2013-05-06'
JOIN air_flight f
ON t.flight_id = f.flight_id
AND f.from_location = 'Chennai'
AND f.to_location = 'Hyderabad'
order by p.profile_id;

Write a query to display the number of tickets as “No_of_Tickets” booked by Kochi


Customers. The Query should display the Profile_Id, First_Name, Base_Location and
number of tickets booked.
Hint: Use String functions to get the base location of customer from their Address and give
alias name as “Base_Location”
Display the records sorted in ascending order based on customer first name.

SELECT cus.profile_id,
cus.First_name,
SUBSTR(cus.address,INSTR(cus.address,',')+1,INSTR(cus.address,'-')-
INSTR(cus.address,',')-1)
AS Base_Location,
s.No_of_Tickets
FROM air_passenger_profilecus JOIN
(
SELECT profile_id,
COUNT(ticket_id) AS No_of_Tickets
FROM air_ticket_info
GROUP BY profile_id
)s
ON s.profile_id = cus.profile_id
AND SUBSTR(cus.address,INSTR(cus.address,',')+1,INSTR(cus.address,'-')-
INSTR(cus.address,',')-1) = 'Kochi'
order by first_name;

Write a query to display the flight_id, from_location, to_location, number of Services as


“No_of_Services” offered in the month of May.
Hint:The number of services can be calculated from the number of scheduled departure dates
of the flight
Display the records sorted in ascending order based on flight id.

SELECT f.flight_id,
f.from_location,
f.to_location,
s.No_of_Services
FROM air_flight f JOIN
(
SELECT flight_id,
COUNT(flight_departure_date) AS No_of_Services
FROM air_flight_details
WHERE MONTH(flight_departure_date) = 5
GROUP BY flight_id
)s
ON f.flight_id = s.flight_id
order by f.flight_id;

Write a query to display profile id,lastname,mobile number and email id of passengers whose
base location is chennai.Display the records sorted in ascending order based on profile id.

SELECT PROFILE_ID,LAST_NAME,MOBILE_NUMBER,EMAIL_ID FROM


air_passenger_profile where address like '%CHENNAI%' ORDER BY PROFILE_ID;

Write a query to display number of flights between 6.00 AM and 6.00 PM from chennai. Hint
Use FLIGHT_COUNT as alias name.

SELECT count(flight_id) FLIGHT_COUNT FROM air_flight where


FROM_LOCATION='CHENNAI' and departure_time between '06:00:00' and '18:00:00';

Write a query to display unique profile id,first name , email id and contact number of
passenger(s) who travelled on flight with id 3148. Display the records sorted in ascending
order based on first name.
SELECT distinct ti.PROFILE_ID,pi.first_name,pi.email_id,pi.mobile_number FROM
air_ticket_infoti join air_passenger_profile pi on pi.profile_id=ti.profile_id where
flight_id=3148 order by pi.first_name;

1)Write a query to display profile id of passenger(s) who booked minimum numberof


tickets.In case of multiple records, display the records sorted in ascending order based
onprofile id.

SELECT profile_id
FROM air_ticket_info
group by profile_id
having count(profile_id)<=all(select
count(profile_id) from air_ticket_info group by profile_id)
order by profile_id;
(OR)
SELECT profile_id
FROM air_ticket_info
GROUP BY profile_id
HAVING count(ticket_id) IN ( SELECT min(c)
FROM (SELECT count(ticket_id) c
FROM air_ticket_info
GROUP BY profile_id)a)
order by profile_id;

2)Write a query to intimate the passengers who are boarding Chennai to Hyderabad Flight on
6th May 013 stating the delay of 1hr in the departure time. The Query should display the
passenger’s profile_id, first_name,last_name, flight_id, flight_departure_date, actual
departure time , actual arrival time , delayed departure time as "Delayed_Departure_Time",
delayed arrival time as "Delayed_Arrival_Time"
Hint: Distinct Profile ID should be displayed irrespective of multiple tickets booked by the
same profile.
Display the records sorted in ascending order based on passenger's profile id.

SELECT DISTINCT p.profile_id,


p.first_name,
p.last_name,
t.flight_id,
t.flight_departure_date,
f.departure_time,
f.arrival_time,
ADDTIME(f.departure_time,'01:00:00') AS Delayed_Departure_Time,
ADDTIME(f.arrival_time,'01:00:00') AS Delayed_Arrival_Time
FROM air_passenger_profile p
JOIN air_ticket_info t
ON p.profile_id = t.profile_id
AND t.flight_departure_date = '2013-05-06'
JOIN air_flight f
ON t.flight_id = f.flight_id
AND f.from_location = 'Chennai'
AND f.to_location = 'Hyderabad'
order by p.profile_id;
(OR)
SELECT p.profile_id,
p.first_name,
p.last_name,
fd.flight_id,
fd.flight_departure_date,
f.departure_time as 'Actual_Departure_Time',
f.arrival_time as 'Actual_Arrival_Time',
addtime(f.departure_time, '01:00:00') as 'Delayed_Departure_Time',
addtime(f.arrival_time, '01:00:00') as 'Delayed_Arrival_Time'
FROM air_passenger_profile p,
air_flight_details fd,
air_ticket_info t,
air_flight f
WHERE p.profile_id=t.profile_id
AND t.flight_id=fd.flight_id
AND fd.flight_id=f.flight_id
AND f.from_location='Chennai'
AND f.to_location='hyderabad'
AND fd.flight_departure_date='2013-05-06'
Order by BY p.profile_id;

3)Write a query to display number of flights between 6.00 AM and 6.00 PM from chennai.
Hint Use
FLIGHT_COUNT as alias name.

SELECT count(flight_id) FLIGHT_COUNT


FROM air_flight
where FROM_LOCATION='CHENNAI'
and departure_time between '06:00:00' and '18:00:00';

4)Write a query to display flight id,from location, to location and ticket price of flights whose
departure is in the month of april. Display the records sorted in ascending order based on
flight id and then by from
location.

SELECT fd.flight_id,af.FROM_LOCATION,af.TO_LOCATION, fd.price


FROM air_flight_details fd
join air_flight af
on af.FLIGHT_ID=fd.FLIGHT_ID
where substring(fd.flight_departure_date,6,2)='04'
order by fd.flight_id, af.FROM_LOCATION;
(OR)
SELECT f.flight_id,
f.from_location,
f.to_location,
fd.price
FROM air_flight f,air_flight_details fd
WHERE f.flight_id=fd.flight_id
AND monthname(fd.flight_departure_date)='APRIL'
order by fd.flight_id, f.FROM_LOCATION;

5)Write a query to display the customer(s) who has/have booked maximum number of tickets
in ABC Airlines. The Query should display profile_id, customer’s first_name, Address and
Number of tickets booked as “No_of_Tickets” Display the records in ascending order based
on customer's first name.

SELECT ti.profile_id,pp.first_name,
pp.address, count(ti.ticket_id) as No_of_Tickets
FROM air_ticket_info ti,
air_flight f,
air_passenger_profile pp
WHERE ti.flight_id=f.flight_id
and ti.profile_id=pp.profile_id
and f.airline_name like 'abc'
GROUP BY profile_id
HAVING count(ti.ticket_id) =( SELECT min(c)
FROM ( SELECT count(ticket_id)c
FROM air_ticket_info ti, air_flight f
WHERE ti.flight_id=f.flight_id
AND f.airline_name like 'abc'
GROUP BY profile_id )a)
ORDER BY pp.first_name;

6)Write a query to display profile id of passenger who booked maximum number of tickets

SELECT profile_id,
count(ticket_id)
FROM air_ticket_info
GROUP BY profile_id
HAVING count(ticket_id) IN ( SELECT max(c)
FROM ( SELECT count(ticket_id) c
FROM air_ticket_info
GROUP BY profile_id)a);

7)Write a query to display unique profile id,first name , email id and contact number of
passenger(s) who travelled on flight with id 3148. Display the records sorted in ascending
order based on first name.

SELECT distinct ti.PROFILE_ID,pi.first_name,pi.email_id,pi.mobile_number


FROM air_ticket_info ti
join air_passenger_profile pi
on pi.profile_id=ti.profile_id
where flight_id=3178
order by pi.first_name; (OR)
SELECT distinct ti.PROFILE_ID,pp.first_name,
pp.email_id,pp.mobile_number
FROM air_ticket_info ti,
air_passenger_profile pp
where pp.profile_id=ti.profile_id
and ti.flight_id=3178
order by pp.first_name;

8)Write a query to display profile id,last name,mobile number and email id of passengers
whose base location is chennai. Display the records sorted in ascending order based on
profile id.

SELECT PROFILE_ID,LAST_NAME,MOBILE_NUMBER,EMAIL_ID
FROM air_passenger_profile
where address like '%CHENNAI%'
ORDER BY PROFILE_ID;

9)Write a query to display the number of tickets as “No_of_Tickets” booked by Kochi


Customers. The Query should display the Profile_Id, First_Name, Base_Location and
number of tickets booked.
Hint: Use String functions to get the base location of customer from their Address and give
alias name as “Base_Location” Display the records sorted in ascending order based on
customer first name.

SELECT p.profile_id,
p.first_name,
substring_index(substring_index(p.address,"-",-2),"-",1) AS 'Base_Location',
COUNT(t.ticket_id)
FROM air_ticket_info t, air_passenger_profile p
WHERE p.profile_id=t.profile_id
AND substring_index(substring_index(p.address,"-",-2),"-",1)='Kochi'
GROUP BY p.profile_id;

10)Write a query to display the flight_id, from_location, to_location, number of Services as


“No_of_Services” offered in the month of May.
Hint:The number of services can be calculated from the number of scheduled departure dates
of the flight
Display the records sorted in ascending order based on flight id.

SELECT f.flight_id,
f.from_location,
f.to_location,
s.No_of_Services
FROM air_flight f JOIN
(
SELECT flight_id,
COUNT(flight_departure_date) AS No_of_Services
FROM air_flight_details
WHERE MONTH(flight_departure_date) = 5
GROUP BY flight_id
)s
ON f.flight_id = s.flight_id
order by f.flight_id;
(OR)
SELECT f.flight_id,
f.from_location,
f.to_location,
count(fd.flight_departure_date) as 'No_of_Services'
FROM air_flight f,air_flight_details fd
WHERE f.flight_id=fd.flight_id
AND monthname(flight_departure_date)='May'
GROUP BY f.flight_id
ORDER BY f.flight_id;

11) Write a query to display unique passenger profile id,first name,mobile number and email
address of assengers who booked ticket to travel from HYDERABAD to CHENNAI. Display
the records sorted in scending order based on profile id.

SELECT distinct ti.PROFILE_ID,pi.first_name,pi.mobile_number, pi.email_id


FROM air_ticket_info ti
join air_passenger_profile pi
on pi.profile_id=ti.profile_id
where flight_id in (SELECT FLIGHT_ID FROM
air_flight where FROM_LOCATION ='HYDERABAD'
and to_location ='CHENNAI')
order by ti.profile_id
(OR)
SELECT distinct ti.PROFILE_ID,pp.first_name,pp.mobile_number, pp.email_id
FROM air_ticket_info ti ,air_passenger_profile pp,air_flight f
WHERE pp.profile_id=ti.profile_id
AND ti.flight_id=f.flight_id
AND FROM_LOCATION ='CHENNAI'
AND to_location ='HYDERABAD'
order by ti.profile_id
NO RECORDS FROM HYDERABAD TO CHENNAI

12)Write a query to display the number of passengers who have travelled in each flight in
each scheduled date. The Query should display flight_id, flight_departure_date and the
number of passengers as “No_of_Passengers” in the same order. Display the records sorted in
ascending order based on flight id and then by flight departure date.
SELECT flight_id,
flight_departure_date,
COUNT(ticket_id) AS No_of_Passengers
FROM air_ticket_info
GROUP BY flight_id,
flight_departure_date
ORDER BY flight_id, flight_departure_date;

13)Write a query to display the no of services offered by each flight and the total price of the
services. The Query should display flight_id, number of services as “No_of_Services” and
the cost as Total_Price” in the same order. Order the result by Total Price in descending
order and then by flight_id in descending order.
Hint:The number of services can be calculated from the number of scheduled departure dates
of the flight

SELECT flight_id,
COUNT(flight_departure_date) AS No_of_Services,
SUM(price) AS Total_Price
FROM air_flight_details
GROUP BY flight_id
order by total_price DESC, flight_id DESC;

14) Write a query to display the customers who have booked tickets from Chennai to
Hyderabad. The query should display profile_id, customer_name (combine first_name &
last_name with comma in b/w), address of the customer. Give an alias to the name as
customer_name.
Hint: Query should fetch unique (so distinct) customers irrespective of multiple tickets
booked. Display the records sorted in ascending order based on profile id.

SELECT DISTINCT c.profile_id,


CONCAT(c.first_name, ',',c.last_name) AS customer_name,
c.address
FROM air_passenger_profile c
JOIN air_ticket_info t
ON c.profile_id = t.profile_id
JOIN air_flight f
ON f.flight_id = t.flight_id
AND f.from_location = 'Chennai'
AND f.to_location = 'Hyderabad'
order by c.profile_id; (or)
SELECT DISTINCT pp.profile_id,
CONCAT(pp.first_name, ',',pp.last_name) AS customer_name,
pp.address
FROM air_passenger_profile pp, air_ticket_info ti, air_flight f
where pp.profile_id = ti.profile_id
and f.flight_id = ti.flight_id
AND f.from_location = 'Chennai'
AND f.to_location = 'Hyderabad'
order by pp.profile_id;

15)Write a query to display the average cost of the tickets in each flight on all scheduled
dates. The query should display flight_id, from_location, to_location and Average price as
“Price”. Display the records sorted in ascending order based on flight id and then by
from_location and then by to_location.

SELECT f.flight_id,
f.from_location,
f.to_location,
AVG(fd.Price) AS Price
FROM air_flight f JOIN
air_flight_details fd
ON f.flight_id = fd.flight_id
GROUP BY f.flight_id order by f.flight_id, f.from_location, f.to_location;
(OR)
SELECT f.flight_id,
f.from_location,
f.to_location, AVG(fd.Price) AS Price
FROM air_flight f ,air_flight_details fd
where f.flight_id = fd.flight_id
GROUP BY f.flight_id
order by f.flight_id, f.from_location, f.to_location;

16) Write a query to display the average monthly ticket cost for each flight in ABC Airlines.
The query should display the Flight_Id,From_location,To_Location,Month Name as
“Month_Name” and average price as “Average_Price” Display the records sorted in
ascending order based on flight id and then by Month Name.

SELECT f.flight_id,
f.from_location,
f.to_location,
fp.Month_Name,
fp.Average_Price
FROM air_flight f,
(
SELECT flight_id,
MONTHNAME(flight_departure_date) AS Month_Name,
AVG(price) as Average_Price
FROM air_flight_details
GROUP BY flight_id,
MONTHNAME(flight_departure_date)
) fp
WHERE f.flight_id = fp.flight_id
and f.airline_name = 'ABC AIRLINES'
order by f.flight_id, fp.Month_Name;
(or)
SELECT f.flight_id,
f.from_location,
f.to_location,
MONTHNAME(fd.flight_departure_date) as MonthName,
AVG(fd.price) as Average_Price
FROM air_flight f,air_flight_details fd
WHERE f.flight_id = fd.flight_id
and f.airline_name = 'ABC AIRLINES'
order by f.flight_id, MONTHNAME(fd.flight_departure_date);

17) Write a query to display flight id,departure date,flight type of all flights. Flight type can
be identified if ticket price is less than 3000 then 'AIR PASSENGER',ticket price between
3000 and less than 4000 'AIR BUS' and ticket price between 4000 and greater than 4000 then
'EXECUTIVE PASSENGER'.
Hint use FLIGHT_TYPE as alias name.

SELECT flight_id,
flight_departure_date,
CASE
WHEN price<3000 THEN 'AIR PASSENGER'
WHEN price>=3000 AND price<4000 THEN 'AIR BUS'
WHEN price>=4000 THEN 'EXECUTIVE PASSENGER'END
as 'FLIGHT_TYPE'
FROM air_flight_details

18)Write a query to display the Flight_Id, Flight_Departure_Date,


From_Location,To_Location and Duration which has duration of travel less than 1 Hour, 10
Minutes
SELECT f.flight_id,
fd.flight_departure_date,
f.from_location,
f.to_location, duration
FROM air_flight f, air_flight_details fd
where f.flight_id=fd.flight_id
and f.duration<'01:30:00';

19)Write a query to display the credit card type and no of credit cards used on the same type.
Hint: Use CARD_COUNT AS Alias name for no of cards.

SELECT c.card_type,
count(c.card_number) as 'CARD_COUNT'
FROM air_credit_card_details c
GROUP BY c.card_type;

20)Write a query to find the no of services offered by each flight and the total price of the
services.
The Query should display flight_id, number of services as “No_of_Services” (alias
name)&nbsp;and the cost as “Total_Price” (alias name). Order the result by Highest Total
Price
Hint:The number of services can be calculated from the number of scheduled departure dates
of the flight

SELECT f.flight_id,
count(fd.flight_departure_date)as 'No_of_Services',
count(t.profile_id)*fd.price as 'Total_Price'
FROM air_flight f,air_flight_details fd,air_ticket_info t
where f.flight_id=fd.flight_id
and t.flight_id=f.flight_id
GROUP BY f.flight_id
ORDER BY Total_Price DESC;

20) Write a query to find the customer who has booked the ticket with the From_Location
different from their Base_Location. The Query should display
flight_id,profile_id,customer_name,base_location and from_location
Hint: Use string functions to get the city value as Base_location from customer address.
Use alias name as “Base_Location”.
Combine First Name and Last Name with a comma in between and give alias name as
“Customer_Name”. Use distinct to display unique records

SELECT f.flight_id,
p.profile_id,
concat(p.first_name, ',', p.last_name) as 'Customer_Name',
substring_index(substring_index(p.address, '-', -2), '-', 1) as 'Base_location',
f.from_location
FROM air_flight f,
air_passenger_profile p,
air_ticket_info t
WHERE f.flight_id=t.flight_id AND
p.profile_id=t.profile_id AND
f.from_location!=substring_index(substring_index(p.address, '-', -2), '-', 1);

21)Write a query to find the number of flights flying from each location. The Query should
display the starting location as "Location", number of flights to other locations as
“No_of_Flights”.
Hint: Get the distinct starting location from the From_Location &amp; To_Location.

SELECT from_location as 'Location',


count(flight_id) as 'No_of_Flight'
FROM air_flight
GROUP BY from_location;
22) Write a query to find the average cost of the tickets in each flight on all scheduled dates.
The query should display flight_id, from_location, to_location and Average price as “Price”
(alias name).

SELECT f.flight_id,
f.from_location,
f.to_location,
avg(fd.price) as 'Price'
FROM air_flight_details fd,
air_flight f
WHERE f.flight_id=fd.flight_id
GROUP BY f.flight_id, fd.flight_departure_date;

23)Write a query to find the number of passengers traveled on each flight on each scheduled
date and total price of the tickets booked. The Query should display flight_id, from_location,
to_location,flight_departure_date,number of passengers as “No_of_Passengers”&nbsp;(alias
name), Total Price of the tickets booked as “Total_Price”&nbsp;(alias name).
Hint: The Number of passengers inclusive of all the tickets booked with single profile id.
"Total_Price" should be calculated as (No_of_Passengers * price of single ticket

SELECT f.flight_id,
f.from_location,
f.to_location,
fd.flight_departure_date,
count(t.profile_id) as 'No_of_Passenger',
sum(fd.price) as 'Total_Price'
FROM air_flight f,
air_flight_details fd,
air_ticket_info t
WHERE f.flight_id=fd.flight_id AND
fd.flight_id=t.flight_id
GROUP BY f.flight_id;

24) Write a query to find the flights available in Morning, After Noon, Evening &amp; Night.
The Query should display the Flight_Id, From_Location, To_Location , Departure_Time,
time of service as "Time_of_Service". Time of Service should be calculated as: From
05:00:01 Hrs to 12:00:00 Hrs - Morning, 12:00:01 to 18:00:00 Hrs -Afternoon, 18:00:01 to
24:00:00 - Evening and 00:00:01 to 05:00:00 –Night

SELECT f.flight_id,
f.from_location,
f.to_location,
f.departure_time,
CASE WHEN f.departure_time between '05:00:01' and '12:00:00' THEN 'Morning'
WHEN f.departure_time between '12:00:01' and '18:00:00' THEN 'Afternoon'
WHEN f.departure_time between '18:00:01' and '24:00:00' THEN 'Evening'
WHEN f.departure_time between '00:00:00' and '05:00:00' THEN 'Night'
END as 'Time_of_Service'
FROM air_flight f;

25) Write a query to find the customers who are travelling together on the same flight. The
Query should display profile_id, first_name,last_name,flight_id, flight_departure_date
,from_location and to_location.
The result should be sorted based on the flight id and flight departure date.
Hint: Distinct profile id should be displayed irrespective of multiple tickets booked.
Display the result only if more than one customers travelling on the same flight.
For Eg: If the profile ID P1 &amp; P2 booked the ticket on flight F1 on May 3rd and 10th
respectively then do not display P1 &amp; P2. If both P1 &amp; P2 booked the ticket on
flight F1 on May 12th (same day) then display P1 &amp; P2 as they will travel together

SELECT DISTINCT f1.profile_id,


f1.first_name,
f1.last_name,
f1.flight_id,
f1.flight_departure_date,
f1.from_location,
f1.to_location
FROM (SELECT p.profile_id,
p.first_name,
p.last_name,
f.flight_id,
t.flight_departure_date,
f.from_location,
f.to_location
FROM air_flight f,
air_flight_details fd,
air_ticket_info t,
air_passenger_profile p
WHERE f.flight_id=fd.flight_id AND
fd.flight_id=t.flight_id AND
p.profile_id=t.profile_id)f1,
(SELECT p.profile_id,
p.first_name,
p.last_name,
f.flight_id,
t.flight_departure_date,
f.from_location,
f.to_location
FROM air_flight f,
air_flight_details fd,
air_ticket_info t,
air_passenger_profile p
WHERE f.flight_id=fd.flight_id AND
fd.flight_id=t.flight_id AND
p.profile_id=t.profile_id)f2
WHERE f1.from_location=f2.from_location AND
f1.to_location=f2.to_location AND
f1.flight_departure_date=f2.flight_departure_date AND
f1.flight_id=f2.flight_id AND
f1.profile_id!=f2.profile_id
ORDER BY f1.flight_id, f1.flight_departure_date;

26) Write a query to find the flight which has least number of services in the month of May.
The Query should fetch flight_id, from_location, to_location, least number of Services as
“No_of_Services”
Hint: Number of services offered can be calculated from the number of scheduled departure
dates of a flight

SELECT f.flight_id,
f.from_location,
f.to_location,
count(fd.flight_departure_date) as 'No_of_Service'
FROM air_flight_details fd,
air_flight f
WHERE f.flight_id=fd.flight_id AND
monthname(fd.flight_departure_date)='May'
GROUP BY f.flight_id
HAVING No_of_Service IN ( SELECT min(c)
FROM ( SELECT count(flight_departure_date) c
FROM air_flight_details
GROUP BY flight_id)a);

27)Write a query to display the total number of tickets as “No_of_Tickets” booked in each
flight in ABC Airlines. The Query should display the flight_id, from_location, to_location
and the number of tickets.
Display only the flights in which atleast 1 ticket is booked. Display the records sorted in
ascending order based on flight id.

SELECT f.flight_id,
f.from_location,
f.to_location,
COUNT(t.ticket_id) AS No_of_Tickets
FROM air_ticket_info t,air_flight f
WHERE f.flight_id = t.flight_id
AND AIRLINE_NAME = 'abc'
GROUP by f.flight_id
HAVING count(f.flight_id)>=1
ORDER BY f.flight_id;

28) Write a query to display the number of flight services between locations in a month. The
Query should display From_Location, To_Location, Month as “Month_Name” and number
of flight services as “No_of_Services”.
Hint: The Number of Services can be calculated from the number of scheduled departure
dates of a flight.
The records should be displayed in ascending order based on From_Location and then by
To_Location and then by month name.

SELECT f.from_location,
f.to_location,
s.Month_Name,
SUM(s.No_of_Services) AS No_of_Services
FROM air_flight f JOIN
(SELECT flight_id,
MONTHNAME(flight_departure_date) AS Month_Name,
COUNT(flight_departure_date) AS No_of_Services
FROM air_flight_details
GROUP BY flight_id,
MONTHNAME(flight_departure_date)
)s
ON s.flight_id = f.flight_id
GROUP BY f.from_location,
f.to_Location,
s.Month_Name
order by f.from_location,f.to_Location, s.Month_Name;

Potrebbero piacerti anche