Assignment MySQL
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;
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;
Page 3 of 5
Q3. Using the CARSHOWROOM database given in the chapter, write the SQL queries for the
following:
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;