Class 12 Computer Science Structured Query Language SQL NCERT Book Exercise Solution


4. Suppose your school management has decided to conduct cricket matches between students of Class XI and Class XII. Students of each class are asked to join any one of the four teams – Team Titan, Team Rockers, Team Magnet and Team Hurricane. During summer vacations, various matches will be conducted between these teams. Help your sports teacher to do the following:

a) Create a database “Sports”.

Answer : CREATE DATABASE Sports;

b) Create a table “TEAM” with following considerations:

i) It should have a column TeamID for storing an integer value between 1 to 9, which refers to unique identification of a team.

ii) Each TeamID should have its associated name (TeamName), which should be a string of length not less than 10 characters.

Answer : CREATE TABLE TEAM (TeamID INTEGER CHECK (TeamID BETWEEN 1 AND 9), TeamName VARCHAR(30) CHECK(Length (TeamName) >= 10) )

c) Using table level constraint, make TeamID as the primary key.

Answer : CREATE TABLE TEAM (TeamID INTEGER CHECK (TeamID BETWEEN 1 AND 9), TeamName VARCHAR(30) CHECK(Length (TeamName) >= 10), PRIMARY KEY(TeamID) );

d) Show the structure of the table TEAM using a SQL statement.

Answer : DESC TEAM;

e) As per the preferences of the students four teams were formed as given below. Insert these four rows in TEAM table:

Row 1: (1, Team Titan)
Row 2: (2, Team Rockers)
Row 3: (3, Team Magnet)
Row 3: (4, Team Hurricane)

Answer : INSERT INTO TEAM VALUES (1, ‘Team Titan’);

INSERT INTO TEAM VALUES (2, ‘Team Rockers’);

INSERT INTO TEAM VALUES (1, ‘Team Magnet’);

INSERT INTO TEAM VALUES (1, ‘Team Hurricane’);

f) Show the contents of the table TEAM using a DML statement.

Answer : SELECT * FROM TEAM;

g) Now create another table MATCH_DETAILS and insert data as shown below. Choose appropriate data types and constraints for each attribute.

Table: MATCH_DETAILS

MatchIDMatchDateFirstTeamIDSecondTeamIDFirstTeamScoreSecondTeamScore
M12018-07-17129086
M22018-07-18344548
M32018-07-19137856
M42018-07-19245667
M52018-07-1814  3287
M62018-07-17236751
Table: MATCH_DETAILS

Answer : CREATE TABLE MATCH_DETAILS (

MatchId CHAR(2) NOT NULL PRIMARY KEY,

MatchDate DATE,

FirstTeamID INTEGER REFERENCES Team( TeamID),

SecondTeamID INTEGER REFERENCES Team(TeamID),

FirstTeamScore INTEGER,

SecondTeamScore INTEGER

);


5. Using the sports database containing two relations (TEAM, MATCH_DETAILS) and Write the queries for the following:

a) Display the MatchID of all those matches where both the teams have scored more than 70.

Answer : SELECT MatchID FROM MATCH_DETAILS WHERE FirstTeamScore > 70 AND SecondTeamScore > 70;

b) Display the MatchID of all those matches where FirstTeam has scored less than 70 but SecondTeam has scored more than 70.

Answer : SELECT MatchID FROM MATCH_DETAILS WHERE FirstTeamScore < 70 AND SecondTeamScore < 70;

c) Display the MatchID and date of matches played by Team 1 and won by it.

Answer : SELECT MatchID, MatchDate FROM MATCH_DETAILS WHERE (FirstTeamId = 1 AND FirstTeamScore > SecondTeamScore) OR (SecondTeamID = 1 AND SecondTeamScore > FirstTeamScore) ;

d) Display the MatchID of matches played by Team 2 and not won by it.

Answer : SELECT MatchID FROM MATCH_DETAILS WHERE SecondTeamScore < FirstTeamScore AND (FirstTeamID = 2 OR SecondTeamID = 2) ;

e) Change the name of the relation TEAM to T_DATA. Also change the attributes TeamID and TeamName to T_ID and T_NAME respectively.

Answer : ALTER TABLE TEAM RENAME TO T_DATA;

ALTER TABLE T_DATA CHANGE TeamID T_ID INTEGER;

ALTER TABLE T_DATA CHANGE TeamName T_Name VARCHAR(30) ;


6. A shop called Wonderful Garments who sells school uniforms maintains a database SCHOOLUNIFORM as shown below.

It consisted of two relations – UNIFORM and COST. They made UniformCode as the primary key for UNIFORM relations. Further, they used UniformCode and Size to be composite keys for COST relation. By analyzing the database schema and database state, specify SQL queries to rectify the following anomalies.

a) M/S Wonderful Garments also keeps handkerchiefs of red colour, medium size of Rs. 100 each.

b) INSERT INTO COST (UCode, Size, Price) values (7, ‘M’,100);

When the above query is used to insert data, the values for the handkerchief without entering its details in the UNIFORM relation is entered. Make a provision so that the data can be entered in the COST table only if it is already there in the UNIFORM table.

Answer : To overcome this anomalies, you need to create a foreign key constraint in cost table. Here UCode of cost table get related with the UniformCode Primary Key of Uniform table.

ALTER TABLE COST
ADD FOREIGN KEY (UCode) REFERENCES UNIFORM(UniformCode);

c) Further, they should be able to assign a new UCode to an item only if it has a valid UName. Write a query to add appropriate constraints to the SCHOOLUNIFORM database.

Answer : To overcome this this problem, you need to specify NOT NULL Constaint to UName column of UNIFORM table. So that you always suppose to enter valid name to uniform, while adding a record in a table.

ALTER TABLE UNIFORM
MODIFY UName VARCHAR(30) NOT NULL;

d) Add the constraint so that the price of an item is always greater than zero.

Answer : Add a CHECK constraint on price filed of COST table.

ALTER TABLE COST
ADD CONSTRAINT CHECK(Price > 0);



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

Write SQL queries for the following:

a) Create the table Product with appropriate data types and constraints.

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:

mysql> 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:

mysql> UPDATE PRODUCT

            SET UPRICE = UPRICE + UPRICE * 0.12

          WHERE MANUFACTURER = ‘DOVE’;

g) Display the total number of products manufactured by each manufacturer.

Answer:

mysql > 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;


8. 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’;

(II)    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’;

e) List the total number of cars having no discount.

Answer:          SELECT * FROM INVENTORY

                    WHERE DISCOUNT IS NULL;


Class 12 Computer Science NCERT Exercise Solution


You cannot copy content of this page

Scroll to Top