You are on page 1of 5

Assignment MySQL

1. Consider the following MOVIE table and write the SQL queries based on it.
Table : Movie
MovieID MovieName Category ReleaseDate ProductionCost BusinessCost
001 Hindi_Movie Musical 2018-04-23 124500 130000
002 Tamil_Movie Action 2016-05-17 112000 118000
003 English_Movie Horror 2017-08-06 245000 360000
004 Bengali_Movie Adventure 2017-01-04 72000 100000
005 Telugu_Movie Action NULL 100000 NULL
006 Punjabi_Movie Comedy NULL 30500 NULL
a) Display all the information from the Movie table.
Answer : SELECT * FROM MOVIE;
b) List business done by the movies showing only MovieID, MovieName and Total_Earning.
Total_Earning to be calculated as the sum of ProductionCost and BusinessCost.
Answer : SELECT MovieID, MovieName, ProductionCost + BusinessCost as “Total_Earning” FROM
MOVIE;
c) List the different categories of movies.
Answer : SELECT DISTINCT Category FROM MOVIE;
OR
SELECT DISTINCT(Category) FROM MOVIE;
d) Find the net profit of each movie showing its MovieID, MovieName and NetProfit. Net Profit is to
be calculated as the difference between Business Cost and Production Cost.
Answer : SELECT MovieID, MovieName, BusinessCost – ProductionCost as “Net Profict” FROM
MOVIE;
e) List MovieID, MovieName and Cost for all movies with ProductionCost greater than 10,000 and
less than 1,00,000.
Answer : SELECT MovieID, MovieName, ProductionCost FROM MOVIE WHERE ProductionCost >
10000 and ProductionCost < 100000;
f) List details of all movies which fall in the category of comedy or action.
Answer : SELECT * FROM MOVIE WHERE Category IN (‘Comedy’, ‘Action’);
g) List details of all movies which have not been released yet.
Answer : SELECT * FROM MOVIE WHERE ReleaseDate IS NULL;

Q2. Consider the following table named “Product”, showing details of products being sold in a
grocery shop.

Page 1 of 5
Write SQL queries for the following:
a) Create the table Product with appropriate data types and constraints.
Answer:CREATE TABLE PRODUCT (Pcode Char(4), Primary Key,PName Varchar(25) Not Null, UPrice
Decimal(10,2), Manufacturer Varchar(30));
b) Identify the primary key in Product.
Answer: PCode
c) List the Product Code, Product name and price in descending order of their product name. If
PName is the same then display the data in ascending order of price.
Answer: SELECT PCODE, PNAME, UPRICE FROM PRODUCT ORDER BY PNAME DESC, UPRICE
ASC;

d) Add a new column Discount to the table Product.


Answer: ALTER TABLE PRODUCT ADD DISCOUNT DECIMAL (10,2);
e) Calculate the value of the discount in the table Product as 10 per cent of the UPrice for all those
products where the UPrice is more than 100, otherwise the discount will be 0.
Answer:UPDATE PRODUCT SET DISCOUNT = UPRICE * 0.10 WHERE UPRICE > 100;
f) Increase the price by 12 per cent for all the products manufactured by Dove.
Answer:UPDATE PRODUCT SET UPRICE = UPRICE + UPRICE * 0.12
WHERE MANUFACTURER = ‘DOVE’;

Page 2 of 5
g) Display the total number of products manufactured by each manufacturer.
Answer:SELECT MANUFACTURER, COUNT(*) FROM PRODUCT GROUP BY MANUFACTURER;

Write the output(s) produced by executing the following queries on the basis of the information given
above in the table Product:
h) SELECT PName, Avg(UPrice) FROM Product GROUP BY Pname;

i) SELECT DISTINCT Manufacturer FROM Product;

j) SELECT COUNT(DISTINCT PName) FROM Product;

k) SELECT PName, MAX(UPrice), MIN(UPrice) FROM Product GROUP BY PName;

Page 3 of 5
Q3. Using the CARSHOWROOM database given in the chapter, write the SQL queries for the
following:

a) Add a new column Discount in the INVENTORY table.


Answer: ALTER TABLE INVENTORY ADD DISCOUNT DECIMAL(10,2);
b) Set appropriate discount values for all cars keeping in mind the following:
(i) No discount is available on the LXI model.
(ii) VXI model gives a 10% discount.
(iii) A 12% discount is given on cars other than LXI model and VXI model.
Answer: (i) UPDATE INVENTORY SET DISCOUNT = NULL WHERE MODEL = ‘LXI’;
(ii) UPDATE INVENTORY SET DISCOUNT = PRICE * 0.10 WHERE MODEL = ‘VXI’;
(iii) UPDATE INVENTORY SET DISCOUNT = PRICE * 0.12 WHERE MODEL NOT IN
(‘LXI’, ‘VXI’)
c) Display the name of the costliest car with fuel type “Petrol”.
Answer: SELECT CARNAME FROM INVENTORY WHERE PRICE = (SELECT MAX(PRICE) FROM
INVENTORY WHERE FUELTYPE = ‘PETROL’ );
d) Calculate the average discount and total discount available on Car4.
Answer: SELECT AVG(DISCOUNT), SUM(DISCOUNT) FROM INVENTORY GROUP BY
CARNAME HAVING CARNAME = ‘CAR4’;

Page 4 of 5
e) List the total number of cars having no discount.
Answer: SELECT * FROM INVENTORY WHERE DISCOUNT IS NULL;
Q4. Consider the following table :
Table : Bank
Id Salary Designation Bankname Cardtype
1 25000 Clerk SBI Credit
2 22000 Clerk SIB Debit
3 18000 Receptionist Axis Credit
4 50000 Manager SIB Credit
5 45000 Manager Axis Debit
6 40000 Manager Axis Debit
7 15000 Receptionist SBI Credit
a. Display the total salary received by each designation.
Ans. Select sum(Salary) , designation from Bank Group by designation;
OUTPUT :
sum(Salary) designation
47000 Clerk
33000 Receptionist
135000 Manager
b. Display the number of people in each category of Cardtype from the table Bank.
Ans. Select Cardtype, count(Cardtype) from Bank Group By Cardtype;

Q5. Consider the following two tables : Student and Book


Table : Student
Admno Name Class
1 Suman 6
2 Ravi 6
3 Sonam 7
Table : Book
Admno B_id Subject
1 B101 English
2 B102 Math
3 B103 Science
a. Write a query to join two tables Student and Book on the basis of field Admission Number.
Ans. Select * from Student, Book where Student.Admno = Book.Admno;
OR
Select * from Student Join Book on Student.Admno = Book.Admno
OR
Select * from Student Natural Join Book
b. Display Admission number, Student Name and Subject from table Student and Book.
Ans. Select Admno, Name, Subject from Student, Book where Student.Admno = Book.Admno;
c. Display Name and Class of a student who is having book of subject Math.
Ans. Select Name, Class, Subject from Student, Book where Student.Admno = Book.Admno;
Page 5 of 5

You might also like