ALLDBQUERIES
ALLDBQUERIES
SELECT account_number,am.customer_number,firstname,lastname,account_opening_date
FROM customer_master cm JOIN account_master am
ON cm.customer_number=am.customer_number
ORDER BY account_number;
Display the records sorted in ascending order based on customer number and then by
account number.
Write a query to display the number of accounts opened in each city .The Query
should display Branch City and number of accounts as No_of_Accounts.For the branch
city where we don’t have any accounts opened display 0. Display the records in
sorted order based on branch city.
select firstname
FROM customer_master cm INNER JOIN account_master am
ON cm.customer_number=am.customer_number
group by firstname
having count(account_number)>1
order by firstname;
FIRSTNAME
AMIT
AVINASH
RAHUL
RAMESH
SELECT td.account_number
FROM account_master am INNER JOIN transaction_details td
ON am.account_number=td.account_number
group by td.account_number
having count(td.transaction_number)>=ALL
(SELECT count(td.transaction_number)
FROM account_master am INNER JOIN transaction_details td
ON am.account_number=td.account_number
group by td.account_number) order by am.account_number;
ACCOUNT_NUMBER
A00001
select branch_name,branch_city
FROM branch_master INNER JOIN account_master
ON branch_master.branch_id=account_master.branch_id
group by branch_name
having count(customer_number)>=ALL
(select count(customer_number)
FROM branch_master INNER JOIN account_master
ON branch_master.branch_id=account_master.branch_id
group by branch_name) order by branch_name;
BRANCH_NAME BRANCH_CITY
ASAF ALI ROAD DELHI
SELECT firstname
FROM customer_master INNER JOIN account_master
ON customer_master.customer_number=account_master.customer_number
GROUP BY firstname
having count(firstname)>=2 order by firstname;
FIRSTNAME
AMIT
AVINASH
RAHUL
RAMESH
.............................................
1.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.
15 rows
select
f.flight_id,f.from_location,f.to_location,monthname(fd.flight_departure_date) as
Month_name,avg(fd.price) as Average_price from air_flight f join air_flight_details
fd
on f.flight_id=fd.flight_id group by f.flight_id,Month_name order by
f.flight_id,Month_name;
FLIGHT_ID FROM_LOCATION TO_LOCATION MONTH_NAME AVERAGE_PRICE
1011 HYDERABAD CHENNAI APRIL 4614.000000
1011 HYDERABAD CHENNAI MAY 3855.500000
1262 HYDERABAD CHENNAI MAY 3444.500000
1265 CHENNAI HYDERABAD APRIL 4086.000000
1265 CHENNAI HYDERABAD MAY 3303.666667
289 CHENNAI KOCHI MAY 3257.750000
3004 BENGALURU CHENNAI MAY 3319.666667
3013 CHENNAI BENGALURU MAY 3257.750000
3148 CHENNAI BENGALURU JUNE 2773.000000
3148 CHENNAI BENGALURU MAY 3052.000000
3241 CHENNAI KOCHI MAY 3303.666667
3244 KOCHI CHENNAI MAY 3371.500000
3307 BENGALURU CHENNAI MAY 3309.000000
916 CHENNAI HYDERABAD APRIL 4086.000000
916 CHENNAI HYDERABAD MAY 3570.666667
2.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.
1 row
select apf.profile_id,apf.first_name,apf.address,count(ati.ticket_id) as
No_of_Tickets
from air_passenger_profile apf
join air_ticket_info ati on apf.profile_id=ati.profile_id group by apf.profile_id
having
count(ati.ticket_id) <=all
(select count(ati.ticket_id) from air_passenger_profile apf
join air_ticket_info ati on apf.profile_id=ati.profile_id group by apf.profile_id)
order by
first_name;
PROFILE_ID FIRST_NAME ADDRESS NO_OF_TICKETS
PFL008 GANESH 45 3RD ST,HYDERABAD-24 1
9 rows
select af.from_location,af.to_location,monthname(afd.flight_departure_date) as
Month_Name,
count(afd.flight_departure_date) as No_of_Services from air_flight af join
air_flight_details afd
on af.flight_id=afd.flight_id group by af.from_location,af.to_location,month_name
order by
from_location,to_location,month_name;
4.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.
1 row
select app.profile_id,app.first_name,app.address,count(ati.ticket_id) as
No_of_Tickets
from air_passenger_profile app
join air_ticket_info ati on app.profile_id=ati.profile_id join air_flight af on
ati.flight_id=af.flight_id
where af.airline_name= ’ABC Airlines’ group by app.profile_id
having count(ati.ticket_id) >= all (select count(ati.ticket_id) from
air_passenger_profile app
join air_ticket_info ati on app.profile_id=ati.profile_id join air_flight af on
ati.flight_id=af.flight_id
where af.airline_name= ’ABC Airlines’ group by app.profile_id) order by
app.first_name;
PROFILE_ID FIRST_NAME ADDRESS NO_OF_TICKETS
PFL009 RAM 119 2ND CROSS ST,ERNAKULAM-12 8
5.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.
3 rows
select
ati.profile_id,app.first_name,app.last_name,ati.flight_id,ati.flight_departure_date
,count(ati.ticket_id)
as No_of_Tickets from air_ticket_info ati join air_passenger_profile app on
ati.profile_id=
app.profile_id join air_flight af on ati.flight_id=af.flight_id
where af.from_location='chennai' and af.to_location='hyderabad' group by
ati.profile_id,
ati.flight_id,ati.flight_departure_date order by
ati.profile_id,
ati.flight_id,ati.flight_departure_date;
PROFILE_ID FIRST_NAME LAST_NAME FLIGHT_ID FLIGHT_DEPARTURE_DATE
NO_OF_TICKETS
PFL001 LATHA SANKAR 1265 2013-04-29 1
PFL004 AARTHI RAMESH 1265 2013-05-29 1
PFL005 SIVA KUMAR 916 2013-05-06 2
6.Write a query to display flight id,from location, to location and ticket price of
flights whose departure is in the month of april.
3 rows
Display the records sorted in ascending order based on flight id and then by from
location.
select af.flight_id,af.from_location,af.to_location,afd.price from air_flight af
join air_flight_details afd on af.flight_id=afd.flight_id
where monthname(afd.flight_departure_date)='april' order by
flight_id,from_location;
7.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.
11 rows
select af.flight_id,af.from_location,af.to_location,avg(afd.price)
from air_flight af join air_flight_details afd
on af.flight_id=afd.flight_id group by af.flight_id,af.from_location,af.to_location
order by af.flight_id,af.from_location,af.to_location;
FLIGHT_ID FROM_LOCATION TO_LOCATION PRICE
1011 HYDERABAD CHENNAI 4108.333333
1262 HYDERABAD CHENNAI 3444.500000
1265 CHENNAI HYDERABAD 3499.250000
289 CHENNAI KOCHI 3257.750000
3004 BENGALURU CHENNAI 3319.666667
3013 CHENNAI BENGALURU 3257.750000
3148 CHENNAI BENGALURU 2959.000000
3241 CHENNAI KOCHI 3303.666667
3244 KOCHI CHENNAI 3371.500000
3307 BENGALURU CHENNAI 3309.000000
916 CHENNAI HYDERABAD 3699.500000
8.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.
11.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.
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.
9 rows
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;t
FLIGHT_ID FLIGHT_DEPARTURE_DATE NO_OF_PASSENGERS
1011 2013-05-09 4
1262 2013-05-20 1
1265 2013-04-29 1
1265 2013-05-29 1
3004 2013-05-02 3
3148 2013-05-21 2
3148 2013-06-01 5
3244 2013-05-03 7
916 2013-05-06 2
15.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.
1 row
select distinct
app.profile_id,app.first_name,app.last_name,ati.flight_id,ati.flight_departure_date
,
af.departure_time,af.arrival_time, af.departure_time
,ADDTIME(af.departure_time,'1:00:00') as Delayed_Departure_Time,
ADDTIME(af.arrival_time,'1:00:00') as Delayed_Arrival_Time from
air_passenger_profile app
join air_ticket_info ati on app.profile_id=ati.profile_id join air_flight af on
ati.flight_id=af.flight_id where ati.flight_departure_date='2013-05-06' order by
app.profile_id;
PROFILE_
ID FIRST
_NAME LAST_NAME FLIGHT
_ID FLIGHT_
DEPARTURE
_DATE DEPARTURE_TIME ARRIVAL
_TIME DELAYED_DEPARTURE_TIME DELAYED_ARRIVAL_TIME
PFL005 SIVA KUMAR 916 2013-05-06 19:55:00 21:00:00 20:55:00
22:00:00
DELAYED_DEPARTURE_TIME DELAYED_ARRIVAL_TIME
20:55:00 22:00:00
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.
2 rows
select ap.profile_id,ap.first_name,substring_index(substring_index(ap.address,',',-
1),'-',1)
as base_location,count(at.ticket_id) as No_of_Tickets from air_passenger_profile
ap join air_ticket_info at
on at.profile_id=ap.profile_id
where substring_index(substring_index(ap.address,',',-1),'-',1) ='kochi'
group by ap.profile_id order by first_name
11 rows
select
af.flight_id,af.from_location,af.to_location,count(afd.flight_departure_date)
as No_of_Services from air_flight af join air_flight_details afd
on af.flight_id=afd.flight_id where month(afd.flight_departure_date)='05'
group by flight_id order by flight_id;
FLIGHT_ID FROM_LOCATION TO_LOCATION NO_OF_SERVICES
1011 HYDERABAD CHENNAI 2
1262 HYDERABAD CHENNAI 2
1265 CHENNAI HYDERABAD 3
289 CHENNAI KOCHI 4
3004 BENGALURU CHENNAI 3
3013 CHENNAI BENGALURU 4
3148 CHENNAI BENGALURU 2
3241 CHENNAI KOCHI 3
3244 KOCHI CHENNAI 2
3307 BENGALURU CHENNAI 4
916 CHENNAI HYDERABAD 3
18.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.
1 row
select count(flight_id) as FLIGHT_COUNT from air_flight where departure_time
between
'6:00:00' and '18:00:00' and from_location='chennai';;
FLIGHT_COUNT
3
19.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.
2 rows
select distinct app.profile_id,app.first_name,app.email_id,app.mobile_number from
air_passenger_profile app
join air_ticket_info ati on app.profile_id=ati.profile_id
where ati.flight_id= 3148 group by app.first_name order by app.first_name;
PROFILE_ID FIRST_NAME EMAIL_ID MOBILE_NUMBER
PFL002 ARUN [email protected] 8094564243
PFL007 GAYATHRI [email protected] 8073245678
20.Write a query to display the flights available in Morning, AfterNoon, Evening &
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
Write a query to display the credit card type and no of credit cards used on the
same type. Display the records sorted in ascending order based on credit card
type.
Hint: Use CARD_COUNT AS Alias name for no of cards.
3 rows
Write a Query to display serial no, first name,mobile number,email id of all the
passengers who holds email address from gmail.com.
The Serial No will be the last three digits of profile ID.
Hint: Use SERIAL_NO as Alias name for serial number.
Display the records sorted in ascending order based on name.
6 rows
select substring(profile_id,4) as SERIAL_NO,first_name,mobile_number,email_id
from air_passenger_profile where email_id like '%gmail.com' order by first_name;
SERIAL_NO FIRST_NAME MOBILE_NUMBER EMAIL_ID
004 AARTHI 9595652530 [email protected]
008 GANESH 9375237890 [email protected]
007 GAYATHRI 8073245678 [email protected]
001 LATHA 9876543210 [email protected]
006 RAMESH 9432198760 [email protected]
005 SIVA 9884416986 [email protected]
Write a query to display the flight(s) 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
If there are multiple flights, display them sorted in ascending order based on
flight id.
4 rows
select
af.flight_id,af.from_location,af.to_location,count(afd.flight_departure_date) as
No_of_Services from air_flight af join air_flight_details afd on
af.flight_id=afd.flight_id where month(afd.flight_departure_date)='05' group by
af.flight_id
having count(afd.flight_departure_date)
<= all (select count(afd.flight_departure_date) from air_flight af join
air_flight_details afd on
af.flight_id=afd.flight_id where month(afd.flight_departure_date)='05' group by
af.flight_id)
order by af.flight_id;
LIGHT_ID FROM_LOCATION TO_LOCATION NO_OF_SERVICES
1011 HYDERABAD CHENNAI 2
1262 HYDERABAD CHENNAI 2
3148 CHENNAI BENGALURU 2
3244 KOCHI CHENNAI 2
FROM_LOCATION NO_OF_FLIGHTS
BENGALURU 2
CHENNAI 6
HYDERABAD 2
KOCHI 1
Write a query to display the number of passengers traveled in each flight in each
scheduled date. The Query should display flight_id,from_location,To_location,
flight_departure_date and the number of passengers as “No_of_Passengers”.
Hint: The Number of passengers inclusive of all the tickets booked with single
profile id.
Display the records sorted in ascending order based on flight id and then by flight
departure date.
9 rows
select
af.flight_id,af.from_location,af.to_location,ati.flight_departure_date,count(ati.ti
cket_id)
as No_of_Passengers from air_flight af join air_ticket_info ati on
af.flight_id=ati.flight_id
group by af.flight_id,ati.flight_departure_date order by
af.flight_id,ati.flight_departure_date;
Display the records sorted in ascending order based on flight id and then by flight
departure date.
14 rows
select
af.flight_id,afd.flight_departure_date,af.from_location,af.to_location,af.duration
from air_flight af join air_flight_details afd on af.flight_id=afd.flight_id
where duration<'1:10:00' group by af.flight_id,afd.flight_departure_date
order by af.flight_id,afd.flight_departure_date;
.......................................................................
SELECT ecd.employee_id,employee_name,
card_issue_date, if(lcd.duration_in_years=0, ‘NO-VALIDITY DATE’,
date_add(ec.card_issue_date, interval duration_in_years year)) as
CARD_VALIDITY_DATE
FROM employee_master em INNER JOIN
employee_card_details ecd
ON em.employee_id=ecd.employee_id
INNER JOIN loan_card_master lcd
ON ecd.loan_id=lcd.loan_id
order by employee_name, CARD_VALID_DATE;
9 rows
select
eid.issue_id,em.employee_id,em.employee_name,im.item_id,im.item_description,eid.iss
ue_date
from employee_issue_details eid join employee_master em on
eid.employee_id=em.employee_id
join item_master im on eid.item_id=im.item_id order by eid.issue_date
desc,eid.issue_id;
25.Write a query to display the employee id, employee name and total valuation for
employee who has issued maximum total valuation of the product. Give the
alias name for total valuation as TOTAL_VALUATION.
<br>[Hint: Suppose an employee E00019 issued item of price 5000, 10000, 12000 and
E00020 issue item of price 2000, 7000, and 1000. So the valuation of items taken by
E00019 is 27000 and for E00020 it is 10000. So the employee id, employee name and
total valuation of E00019 should display. ]
1 row
select em.employee_id,em.employee_name,sum(im.item_valuation) as TOTAL_VALUATION
from employee_master em join employee_issue_details eid on
em.employee_id=eid.employee_id
join item_master im on eid.item_id=im.item_id group by em.employee_id having
sum(im.item_valuation)
>= all (select sum(im.item_valuation) from employee_master em join
employee_issue_details eid on em.employee_id=eid.employee_id
join item_master im on eid.item_id=im.item_id group by em.employee_id);;
..................................................................................
Write a query to display customer id, customer name,contact number, movie category
and number of movies issued to each customer based on movie category who has been
issued with more than one movie in that category. Example: Display contact number
as "+91-876-456-2345" format.
Hint:Use NO_OF_MOVIES as alias name for number of movies column.
Hint:Use CONTACT_ISD as alias name for contact number.
Display the records sorted in ascending order based on customer name and then by
movie category.
5 rows
select cid.customer_id,cm.customer_name,
concat('+91-',substring(cm.contact_no,1,3),'-',substring(cm.contact_no,4,3),'-',
substring(cm.contact_no,7,4)) as CONTACT_ISD,
mm.movie_category,count(mm.movie_category) as NO_OF_MOVIES from customer_master
cm join customer_issue_details cid
on cm.customer_id=cid.customer_id join movies_master mm on cid.movie_id=mm.movie_id
group by mm.movie_category,cm.customer_name having count(movie_category)>1
order by cm.customer_name,mm.movie_category;
CUSTOMER_ID CUSTOMER_NAME CONTACT_ISD MOVIE_CATEGORY NO_OF_MOVIES
C00002 AGNESH +91-892-315-6781 ACTION 2
C00001 NITIN +91-983-035-4218 ACTION 2
C00004 RAJIB MITRA +91-983-035-6781 COMEDY 3
C00003 T RAMACHANDRAN +91-983-128-9761 ACTION 3
C00003 T RAMACHANDRAN +91-983-128-9761 ROMANCE 4
Write a query to display the customer id , customer name and number of times
movies have been issued from Comedy category. Display only for customers who has
taken more than once.
Hint: Use NO_OF_TIMES as alias name
Display the records in ascending order based on customer name.
1 row
select cm.customer_id,cm.customer_name,count(mm.movie_id) as NO_OF_TIMES from
customer_master cm
join customer_issue_details cid on cm.customer_id=cid.customer_id join
movies_master mm on cid.movie_id=mm.movie_id where mm.movie_category='comedy' group
by customer_id
order by customer_name>1;
CUSTOMER_ID CUSTOMER_NAME NO_OF_TIMES
C00004 RAJIB MITRA 3
Write a query to display customer id and total rent paid by the customers who are
issued with the videos. Need not display the customers who has not taken / issued
with any videos. Hint: Alias Name for total rent paid is TOTAL_COST. Display the
records sorted in ascending order based on customer id
6 rows
select cid.customer_id,sum(mm.rental_cost) as TOTAL_COST from
customer_issue_details cid
join movies_master mm
on cid.movie_id=mm.movie_id group by customer_id order by customer_id;
.........................