SQL Final
SQL Final
use bank;
CUSTOMER_NUMBER VARCHAR(6),
FIRSTNAME VARCHAR(30),
middlename VARCHAR(30),
lastname VARCHAR(30),
CUSTOMER_CITY VARCHAR(15),
CUSTOMER_CONTACT_NO VARCHAR(10),
occupation VARCHAR(10),
CUSTOMER_DATE_OF_BIRTH DATE,
branch_id VARCHAR(6),
branch_name VARCHAR(30),
branch_city VARCHAR(30),
(account_number VARCHAR(255),
customer_number VARCHAR(255),
branch_id VARCHAR(255),
opening_balance INT(20),
account_opening_date DATE,
account_type VARCHAR(10),
account_status VARCHAR(10),
transaction_number VARCHAR(6),
account_number VARCHAR(6),
date_of_transaction DATE,
medium_of_transaction VARCHAR(20),
transaction_type VARCHAR(20),
transaction_amount INT(7),
(customer_number varchar(255),
branch_id varchar(255),
loan_amount bigint(20),
BRANCH MASTER
LOAN DETAILS
TRANSACTION DETAILS
QUERIES
SELECT a.account_number,c.customer_number,c.firstname,c.lastname,a.account_number
FROM customer_master c JOIN account_master a ON
c.customer_number=a.customer_number
ORDER BY a.account_number;
2. Write a query to display the number of customer’s from Delhi. Give the count an alias name of
Cust_Count.
3. Write a query to display the customer number, customer firstname, account number for the
customer’s whose accounts were created after 15th of any month. Display the records sorted in
ascending order based on customer number and then by account number.
SELECT c.customer_number,c.firstname,a.account_number
ON c.customer_number=a.customer_number
WHERE a.account_status='Terminated'
ORDER BY c.customer_number,a.account_number;
5. Write a query to display the total number of withdrawals and total number of deposits being done
by customer whose customer number ends with 001. The query should display transaction type and
the number of transactions. Give an alias name as Trans_Count for number of transactions. Display
the records sorted in ascending order based on transaction type.
ON am.account_number=td.account_number
GROUP BY transaction_type
ORDER BY transaction_type;
6. Write a query to display the number of customers who have registration but no account in the
bank. Give the alias name as Count_Customer for number of customers.
7. Write a query to display account number and total amount deposited by each account holder
(Including the opening balance). Give the total amount deposited an alias name of Deposit_Amount.
Display the records in sorted order based on account number.
SELECT a.account_number,a.opening_balance+sum(t.transaction_amount)
8. 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.
ON account.branch_id=branch.branch_id
10. Write a query to display the customer number, customer firstname, customer lastname who has
taken loan from more than 1 branch. Display the records sorted in order based on customer number.
ORDER BY c.customer_number;
11. Write a query to display the customer’s number, customer’s firstname, customer’s city and branch
city where the city of the customer and city of the branch is different. Display the records sorted in
ascending order based on customer number.
WHERE b.branch_city<>c.customer_city
ORDER BY c.customer_number;
12. Write a query to display the number of clients who have asked for loans but they don’t have any
account in the bank though they are registered customers. Give the count an alias name of Count.
ON c.customer_number=l.customer_number
13. Write a query to display the account number who has done the highest transaction. For example
the account A00023 has done 5 transactions i.e. suppose 3 withdrawal and 2 deposits. Whereas the
account A00024 has done 3 transactions i.e. suppose 2 withdrawals and 1 deposit. So account number
of A00023 should be displayed. In case of multiple records, display the records sorted in ascending
order based on account number.
GROUP BY account_number
HAVING count(transaction_number)>=ALL
14. Write a query to show the branch name,branch city where we have the maximum customers. For
example the branch B00019 has 3 customers, B00020 has 7 and B00021 has 10. So branch id B00021 is
having maximum customers. If B00021 is Koramangla branch Bangalore, Koramangla branch should
be displayed along with city name Bangalore. In case of multiple records, display the records sorted in
ascending order based on branch name.
ORDER BY b.branch_name;
15. Write a query to display all those account number, deposit, withdrawal where withdrawal is more
than deposit amount. Hint: Deposit should include opening balance as well. For example A00011
account opened with Opening Balance 1000 and A00011 deposited 2000 rupees on 2012-12-01 and
3000 rupees on 2012-12-02. The same account i.e A00011 withdrawn 3000 rupees on 2013-01-01 and
7000 rupees on 2013-01-03. So the total deposited amount is 6000 and total withdrawal amount is
10000. So withdrawal amount is more than deposited amount for account number A00011. Display
the records sorted in ascending order based on account number.
SELECT td.account_number,
+(SELECT opening_balance
FROM transaction_details td
GROUP BY td.account_number
ORDER BY td.account_number;
(or)
RIGHT JOIN
(SELECT a.account_number,a.opening_balance+sum(t.transaction_amount) d
ON t1.account_number=t2.account_number
WHERE ifnull(t1.w,0)>t2.d
ORDER BY account_number;
16. Write a query to show the balance amount for account number that ends with 001. Note: Balance
amount includes account opening balance also. Give alias name as Balance_Amount. For example
A00015 is having an opening balance of 1000. A00015 has deposited 2000 on 2012-06-12 and
deposited 3000 on 2012-07-13. The same account has drawn money of 500 on 2012-08-12 , 500 on
2012-09-15, 1000 on 2012-12-17. So balance amount is 4000 i.e (1000 (opening balance)+2000+3000 )
– (500+500+1000).
(or)
RIGHT JOIN
(SELECT a.account_number,a.opening_balance+sum(t.transaction_amount) d
ON t1.account_number=t2.account_number
ORDER BY account_number;
17. Display the customer number, customer's first name, account number and number of transactions
being made by the customers from each account. Give the alias name for number of transactions as
Count_Trans. Display the records sorted in ascending order based on customer number and then by
account number.
18. Write a query to display the customer’s firstname who have multiple accounts (atleast 2
accounts). Display the records sorted in ascending order based on customer's firstname.
ORDER BY c.firstname;
19. Write a query to display the customer number, firstname, lastname for those client where total
loan amount taken is maximum and at least taken from 2 branches. For example the customer C00012
took a loan of 100000 from bank branch with id B00009 and C00012 Took a loan of 500000 from bank
branch with id B00010. So total loan amount for customer C00012 is 600000. C00013 took a loan of
100000 from bank branch B00009 and 200000 from bank branch B00011. So total loan taken is
300000. So loan taken by C00012 is more then C00013.
ON cm.customer_number=ld.customer_number
GROUP BY customer_number
20. Write a query to display the customer’s number, customer’s firstname, branch id and loan amount
for people who have taken loans. Display the records sorted in ascending order based on customer
number and then by branch id and then by loan amount.
ORDER BY c.customer_number,l.branch_id,l.loan_amount;
21. Write a query to display city name and count of branches in that city. Give the count of branches
an alias name of Count_Branch. Display the records sorted in ascending order based on city name.
ORDER BY branch_city;
22. Write a query to display account id, customer’s firstname, customer’s lastname for the customer’s
whose account is Active. Display the records sorted in ascending order based on account id /account
number.
23. Write a query to display customer’s number, first name and middle name. For the customers who
don’t have middle name, display their last name as middle name. Give the alias name as
Middle_Name. Display the records sorted in ascending order based on customer number.
24. Write a query to display the customer number , firstname, customer’s date of birth . Display the
records sorted in ascending order of date of birth year and within that sort by firstname in ascending
order.
ORDER BY c.firstname,a.account_number;
AIRLINES
create database flight;
use flight;
CREATE TABLEair_credit_card_details
card_type VARCHAR(45),
expiration_month INT,
expiration_year INT
);
CREATE TABLEair_passenger_profile
);
CREATE TABLEair_ticket_info
);
CREATE TABLEair_flight_details
);
CREATE TABLEair_flight
);
AIR FLIGHT
QUERIES
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.
SELECT f.flight_id,f.from_location,f.to_location,
monthname(af.flight_departure_date) Month_Name,
2.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,
monthname(af.flight_departure_date) Month_Name,
count(af.flight_departure_date) No_of_Services
ON f.flight_id = af.flight_id
GROUP BY f.from_location,f.to_location,Month_Name
ORDER BY f.from_location,f.to_Location,Month_Name;
3.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 count(ticket_id)
ORDER BY ap.first_name;
4. 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 ap.profile_id,ap.first_name,ap.last_name,af.flight_id,ati.flight_departure_date,
GROUP BY ati.flight_id,ati.profile_id
ORDER BY ap.profile_id,af.flight_id,ati.flight_departure_date;
5. 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.
and month(afd.flight_departure_date)='04'
ORDER BY af.flight_id,af.from_location;
6. 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.
GROUP BY af.flight_id
ORDER BY af.flight_id,af.from_location,af.to_location;
7. 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.
GROUP BY ati.profile_id
ORDER BY ap.profile_id;
8. 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.
group by profile_id
from air_ticket_info
9. 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.
f.flight_id,f.from_location,f.to_location
having count(t.ticket_id)>=1
ORDER by f.flight_id;
10. 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
11. 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.
ORDER BY flight_id,flight_departure_date;
12. 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.
ORDER BY profile_id;
13. Write a query to display unique passenger profile id, first name, 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.
ORDER BY profile_id;
14. 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.
af.departure_time,af.arrival_time,
addtime(af.departure_time,'01:00:00') Delayed_Deparuture_Time,
and ati.flight_departure_date='2013-05-06'
ORDER BY profile_id;
15. 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 ap.profile_id,ap.first_name,
substring_index(substring_index(ap.address,'-',2),'-',-1) Base_Location,
ORDER BY ap.first_name;
16. 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.
WHERE month(flight_departure_date)='05'
GROUP BY af.flight_id,af.from_location,af.to_location
ORDER BY af.flight_id;
17. 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.
FROM air_passenger_profile
ORDER BY profile_id;
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.
WHERE from_location='CHENNAI'
WHERE ati.flight_id='3178'
ORDER BY ap.first_name;
20. Write a query to display flight id,departure date,flight type of all flights. Flight type can be
identified based on the following rules : 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.Display the records sorted in
ascendeing order based on flight_id and then by departure date.
SELECT flight_id,flight_departure_date,
ORDER BY flight_id,flight_departure_date;
21. 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.
22.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.
air_passenger_profile
ORDER BY first_name;
23. 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.
WHERE monthname(afd.flight_departure_date)='May'
WHERE monthname(flight_departure_date)='May'
GROUP BY flight_departure_date)
ORDER BY flight_id;
24. 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 - NightDisplay the records sorted in ascending order based on flight id.
SELECT flight_id,from_location,to_location,Departure_Time,
CASE
THEN 'Morning'
THEN 'AfterNoon'
THEN 'Evening'
WHEN departure_time='00:00:00'
THEN 'Evening'
THEN 'Night'
END Time_of_Service
FROM air_flight
order by flight_id;
25. Write a query to display the number of flights flying from each location. The Query should display
the from location and the number of flights to other locations as “No_of_Flights”. Hint: Get the
distinct from location and to location.Display the records sorted in ascending order based on from
location.
ORDER BY from_location;
26. 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.
SELECT af.flight_id,af.from_location,af.to_location,ati.flight_departure_date,
GROUP BY af.flight_id,af.from_location,af.to_location,ati.flight_departure_date
ORDER BY af.flight_id,ati.flight_departure_date;
27. Write a query to display the flight details in which more than 10% of seats have been booked. The
query should display Flight_Id, From_Location, To_Location,Total_Seats, seats booked as
“No_of_Seats_Booked” .Display the records sorted in ascending order based on flight id and then by
No_of_Seats_Booked.
SELECT af.flight_id,af.from_location,af.to_location,af.total_seats,
WHERE (af.total_seats-afd.available_seats)>(af.total_seats*0.1)
ORDER BY flight_id,No_of_Seats_Booked;
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 af.duration<'01:10:00';
SELECT afd.flight_id,af.from_location,af.to_location,
GROUP BY af.flight_id,af.from_location,af.to_location
ORDER BY average_price;
MOVIE
CREATE DATABASE video;USE video;
Create table CUSTOMER_MASTER
(CUSTOMER_ID Varchar(10),CUSTOMER_NAME Varchar(30) NOT NULL,CONTACT_NO
BIGINT(10),CONTACT_ADD Varchar(20),DATE_OF_REGISTRATION Date NOT NULL,AGE
Varchar(15)NOT NULL,Constraint MT_cts1 PRIMARY KEY(CUSTOMER_ID));
Create table LIBRARY_CARD_MASTER
(CARD_ID Varchar(10),DESCRIPTION Varchar(30) NOT NULL,AMOUNT
BIGINT(50),NUMBER_OF_YEARS bigint(10) NOT NULL,Constraint MT_cts2 PRIMARY
KEY(CARD_ID));
MOVIE MASTER
CUSTOMER MASTER
1.Write a query to display movie names and number of times that movie is issued to
customers. Incase movies are never issued to customers display number of times as 0. Display
the details in sorted order based on number of times (in descending order) and then by
movie name (in ascending order). The Alias name for the number of movies issued is
ISSUE_COUNT.
SELECT m.MOVIE_NAME,count(ISSUE_ID) ISSUE_COUNT FROM
movies_master m LEFT JOIN customer_issue_details c ON m.MOVIE_ID=c.MOVIE_ID
GROUP BY m.movie_name
ORDER BY ISSUE_COUNT DESC,MOVIE_NAME;
SELECT CUSTOMER_ID,CUSTOMER_NAME,AGE,ifnull(
concat('+91-',substring(contact_no,1,3),'-',
substring(contact_no,4,3),'-',substring(contact_no,7)),'N/A') CONTACT_ISD
FROM customer_master WHERE age>25 and year(date_of_registration)='2012'
ORDER BY age,CUSTOMER_NAME;
3.Write a query to display the movie category and number of movies in that category. Display
records based on number of movies from higher to lower order and then by movie category
in ascending order.Hint: Use NO_OF_MOVIES as alias name for number of movies.
SELECT MOVIE_CATEGORY,count(MOVIE_ID) NO_OF_MOVIES FROM
movies_master GROUP BY MOVIE_CATEGORY
ORDER BY NO_OF_MOVIES DESC,MOVIE_CATEGORY;
4.Write a query to display the number of customers having card with description “Gold card”.
<br/>Hint: Use CUSTOMER_COUNT as alias name for number of customers
SELECT count(c.customer_id) CUSTOMER_COUNT FROM
library_card_master l JOIN customer_card_details c ON l.CARD_ID=c.CARD_ID
WHERE description='Gold';
5.Write a query to display the customer id, customer name, year of registration,library card
id, card issue date of all the customers who hold library card. Display the records sorted by
customer name in descending order. Use REGISTERED_YEAR as alias name for year of
registration.
SELECT c.customer_id,c.customer_name,
year(c.DATE_OF_REGISTRATION) REGISTERED_YEAR,cd.card_id,cd.issue_date FROM
customer_master c JOIN customer_card_details cd ON c.customer_id=cd.customer_id
ORDER BY CUSTOMER_NAME DESC;
6.Write a query to display issue id, customer id, customer name for the customers who have
paid fine and whose name starts with 'R'. Fine is calculated based on return date and actual
date of return. If the date of actual return is after date of return then fine need to be paid by
the customer order by customer name.
SELECT ci.issue_id,ci.CUSTOMER_ID,c.CUSTOMER_NAME FROM
7. Write a query to display customer id, customer name, card id, card description and card
amount in dollars of customers who have taken movie on the same day the library card is
registered. For Example Assume John registered a library card on 12th Jan 2013 and he took a
movie on 12th Jan 2013 then display his details. AMOUNT_DOLLAR = amount/52.42 and
round it to zero decimal places and display as $Amount. Example Assume 500 is the amount
then dollar value will be $10. Hint: Use AMOUNT_DOLLAR as alias name for amount in dollar.
Display the records in ascending order based on customer name.
SELECT c.CUSTOMER_ID,c.CUSTOMER_NAME,l.card_id,l.DESCRIPTION,
concat('$',round(amount/52.42)) AMOUNT_DOLLAR FROM
customer_master c JOIN customer_issue_details ci ON c.customer_id=ci.customer_id
JOIN customer_card_details cc ON cc.customer_id=c.customer_id
8.Write a query to display the customer id, customer name,contact number and address of
customers who have taken movies from library without library card and whose address ends
with 'Nagar'. Display customer name in upper case. Hint: Use CUSTOMER_NAME as alias
name for customer name. Display the details sorted in ascending order based on customer
name.
SELECT CUSTOMER_ID,upper(CUSTOMER_NAME) CUSTOMER_NAME,contact_no,contact_add
FROM
customer_master WHERE contact_add LIKE '%Nagar' and
9.Write a query to display the movie id, movie name,release year,director name of movies
acted by the leadactor1 who acted maximum number of movies .Display the records sorted in
ascending order based on movie name.
10.Write a query to display the customer name and number of movies issued to that
customer sorted by customer name in ascending order. If a customer has not been issued
with any movie then display 0. <br>Hint: Use MOVIE_COUNT as alias name for number of
movies issued.
SELECT c.customer_name,count(ci.movie_id) MOVIE_COUNT FROM
13.Write a query to display movie id ,movie name and actor names of movies which are not
issued to any customers. <br> Actors Name to be displayed in the below
format.LEAD_ACTOR_ONE space ambersant space LEAD_ACTOR_TWO.Example: Assume lead
actor one's name is "Jack Tomson" and Lead actor two's name is "Maria" then Actors name
will be "Jack Tomsom & Maria"Hint:Use ACTORS as alias name for actors name. <br> Display
the records in ascending order based on movie name.
14.Write a query to display the director's name, movie name and lead_actor_name1 of all the
movies directed by the director who directed more than one movie. Display the directors
name in capital letters. Use DIRECTOR_NAME as alias name for director name column Display
the records sorted in ascending order based on director_name and then by movie_name in
descending order.
15.Write a query to display number of customers who have registered in the library in the
year 2012 and who have given/provided contact number. <br> Hint:Use NO_OF_CUSTOMERS
as alias name for number of customers.
SELECT count(customer_id) NO_OF_CUSTOMER FROM customer_master
WHERE contact_no is not null and year(date_of_registration)='2012';
16.Write a query to display the customer's name, contact number,library card id and library
card description of all the customers irrespective of customers holding a library card. If
customer contact number is not available then display his address. Display the records sorted
in ascending order based on customer name. Hint: Use CONTACT_DETAILS as alias name for
customer contact.
SELECT c.customer_name,ifnull(c.contact_no,c.contact_add)
CONTACT_DETAILS,l.card_id,l.description FROM
customer_master c LEFT JOIN customer_card_details cc ON c.customer_id=cc.customer_id
17. Write a query to display the customer id, customer name and number of times the same
movie is issued to the same customers who have taken same movie more than once. Display
the records sorted by customer name in decending order For Example: Assume customer
John has taken Titanic three times and customer Ram has taken Die hard only once then
display the details of john. Hint: Use NO_OF_TIMES as alias name for number of times
SELECT ci.customer_id,c.customer_name,count(ci.movie_id) NO_OF_TIMES FROM
customer_issue_details ci JOIN customer_master c ON c.customer_id=ci.customer_id
GROUP BY ci.customer_id,ci.movie_id HAVING count(movie_id)>1
18.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.
SELECT c.customer_id,c.customer_name,concat('+91-',substring(c.contact_no,1,3),'-',
substring(c.contact_no,4,3),'-',substring(c.contact_no,7)) CONTACT_ISD
,m.movie_category,count(cc.movie_id) NO_OF_MOVIES FROM customer_master c JOIN
customer_issue_details cc
19.Write a query to display customer id and customer name of customers who has been
issued with maximum number of movies and customer who has been issued with minimum
no of movies. For example Assume customer John has been issued 5 movies, Ram has been
issued 10 movies and Kumar has been issued 2 movies. The name and id of Ram should be
displayed for issuing maximum movies and Kumar should be displayed for issuing minimum
movies. Consider only the customers who have been issued with atleast 1 movie Customer(s)
who has/have been issued the maximum number of movies must be displayed first followed
by the customer(s) who has/have been issued with the minimum number of movies. In case
of multiple customers who have been displayed with the maximum or minimum number of
movies, display the records sorted in ascending order based on customer name.
SELECT cid.customer_id , customer_name FROM customer_master cm JOIN
customer_issue_details cidON cm.customer_id=cid.customer_id
GROUP BY customer_id , customer_name
HAVING count(movie_id)>=ALL(SELECT count(movie_id)
FROM customer_issue_details
GROUP BY customer_id)
UNION
SELECT cid.customer_id , customer_name FROM
customer_master cm JOIN customer_issue_details cid
ON cm.customer_id=cid.customer_id
20.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.
SELECT c.customer_id,c.customer_name,count(m.movie_id) NO_OF_TIMES FROM
customer_master c JOIN customer_issue_details cc ON c.customer_id=cc.customer_id
JOIN movies_master m ON m.movie_id=cc.movie_id
WHERE m.movie_category='Comedy'
21.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
SELECT cid.customer_id, sum(m.rent_cost) TOTAL_COST FROM customer_issue_details cid
JOIN movies_master mm ON cid.movie_id=mm.movie_id GROUP BY cid.customer_id order by
customer_id;
LOAN
create database loan;
use loan;
(
loan_id varchar(6) PRIMARY KEY,
loan_type varchar(15),
duration_in_years int(2)
);
employee_name varchar(20),
designation varchar(25),
department varchar(25),
gender char(1),
date_of_birth date,
date_of_joining date
);
item_valuation int(6)
);
(
employee_id varchar(6) REFERENCES employee_master,
loan_id varchar(6) REFERENCES loan_card_master,
card_issue_date date
);
EMPLOYEE MASTER
ITEM MASTER
1. Write a query to display category and number of items in that category. Give the count an alias
name of Count_category. Display the details on the sorted order of count in descending order.
2. Write a query to display the number of employees in HR department. Give the alias name as
No_of_Employees.
3. Write a query to display employee id, employee name, designation and department for employees
who have never been issued an item as a loan from the company. Display the records sorted in
ascending order based on employee id.
ORDER BY employee_id;
4. Write a query to display the employee id, employee name who was issued an item of highest
valuation. In case of multiple records, display the records sorted in ascending order based on
employee id.[Hint Suppose an item called dinning table is of 22000 and that is the highest price of the
item that has been issued. So display the employee id and employee name who issued dinning table
whose price is 22000.]
5. Write a query to display issue_id, employee_id, employee_name. Display the records sorted in
ascending order based on issue id.
7. Write a query to count the number of cards issued to an employee “Ram”. Give the count an alias
name as No_of_Cards.
(or)
ON c.employee_id = e.employee_id
8. Write a query to display the count of customers who have gone for loan type stationary. Give the
count an alias name as Count_stationary.
9. Write a query to display the employee id, employee name and number of items issued to them.
Give the number of items an alias name as Count. Display the details in descending order of count and
then
10. Write a query to display the employee id, employee name who was issued an item of minimum
valuation.In case of multiple records, display them sorted in ascending order based on employee
id.[Hint Suppose an item called pen is of rupees 20 and that is the lowest price. So display the
employee id and employee name who issued pen where the valuation is 20.]
ORDER BY employee_id;
11. Write a query to display the employee id, employee name and total valuation of the product
issued to each employee. Give the alias name as TOTAL_VALUATION.Display the records sorted in
ascending order based on employee id.Consider only employees who have been issued atleast 1 item.
WHERE datediff(ei.return_date,ei.issue_date)>365
ORDER BY employee_id;
13. Write a query to display employee id, employee name and count of items of those who asked for
more than 1 furniture. Give the alias name for count of items as COUNT_ITEMS.Display the records
sorted in ascending order on employee id.
WHERE i.item_category='Furniture'
15. Write a query to display employee id, employee name who joined the company after 2005.
Display the records sorted in ascending order based on employee id.
WHERE year(date_of_joining)>'2005'
ORDER BY employee_id;
16. Write a query to get the number of items of the furniture category issued and not issued. The
query should display issue status and the number of furniture as No_of_Furnitures.Display the records
sorted in ascending order based on issue_status.
17. Write a query to find the number of items in each category, make and description. The Query
should display Item Category, Make, description and the number of items as No_of_Items. Display the
records in ascending order based on Item Category, then by item make and then by item description.
ORDER BY item_category,item_make,item_description;
18. Write a query to display employee id, employee name, item id and item description of employees
who were issued item(s) in the month of January 2013. Display the records sorted in order based on
employee id and then by item id in ascending order.
ORDER BY employee_id,item_id;
19. Write a query to display the employee id, employee name and count of item category of the
employees who have been issued items in at least 2 different categories.Give the alias name for
category count as COUNT_CATEGORY.Display the records sorted in ascending order based on
employee id.
HAVING COUNT_CATEGORY>=2
ORDER BY employee_id;
20. Write a query to display the item id , item description which was never issued to any employee.
Display the records sorted in ascending order based on item id.
ORDER BY item_id;
21. Write a query to display the employee id, employee name andtotal valuationfor the employees
who has issued minimum total valuation of the product. Give the alias name for total valuation as
TOTAL_VALUATION.[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 of E00020 should be displayed. ]
GROUP BY e.employee_id);
22. Write a query to display the employee id, employee name, card issue date and card valid
date.Order by employee name and then by card valid date. Give the alias name to display the card
valid date as CARD_VALID_DATE.[Hint: Validity in years for the loan card is given in loan_card_master
table. Validity date is calculated by adding number of years in the loan card issue date. If the duration
of year is zero then display AS 'No Validity Date'. ]
SELECT e.employee_id,e.employee_name,card_issue_date,
case
FROM
ORDER BY employee_name,CARD_VALID_DATE;
23. Write a query to display the employee id, employee name who have not issued with any item in
the year 2013. Hint: Exclude those employees who was never issued with any of the items in all the
years. Display the records sorted in ascending order based on employee id.
WHERE year(issue_date)='2013')
ORDER BY employee_id;
24. Write a query to display issue id, employee id, employee name, item id, item description and issue
date. Display the data in descending order of date and then by issue id in ascending order.
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.[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. ]
GROUP BY e.employee_id);