Chapter 8 Solution Ncert Ip Class 11-1
Chapter 8 Solution Ncert Ip Class 11-1
in
YouTube: www.youtube.com/c/techtipnow
Chapter-8 Solution
Introduction to Structured Query Language (SQL)
1. Math the following clauses with their respective functions.
1
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
i) Error
ii) No Error
iii) Depends on Compiler
iv) Successful compilation of the query
Ans: i. Error
(Because we cannot use ‗VALUE‘ keyword multiple times with single INSERT clause
and also we are trying to insert value 109 two times in student_id column which is not
possible because it is PRIMARY KEY)
e. In the following query how many rows will be deleted?
DELETE student
WHERE student_id =109;
i) 1 row
ii) All the rows where student ID is equal to 109
2
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
Ans: i. 1 row
(Because student_id is a primary key and so it has all unique values.)
a. _______ declares that an index in one table is related to that in another table.
i. Primary key
ii. Foreign Key
iii. Composite Key
iv. Secondary Key
4. Consider the following MOVIE database and answer the SQL queries based on it.
3
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
b. List business done by the movies showing only MovieID, MovieName and
BusinessCost.
FROM MOVIE
d. Find the net profit of each movie showing its ID, Name and Net Profit.
(Hint: NetProfit = BusinessCost –ProductionCost)
Make sure that the new column name is labeled as NetProfit. Is this column now
a part of the MOVIE relation. If no, then what name coined for such columns?
What can you say about the profit of a movie which has not yet released? Does
your query result show profit as zero?
(In this query we are finding the NetProfit by subtracting ProductionCost from
BusinessCost and naming the subtraction as NetProft. Such columns which are
not part of table are called Generated Columns. Because we cannot find the
profit of movie which has not yet released, therefore we have set the criteria in
WHERE clause to ReleaseDate IS NOT Null.
e. List all movies with ProductionCost greater than 80,000 and less than 1,25000
showing ID, Name and ProductionCost.
4
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
5. 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:
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 4: (4, Team Hurricane)
Ans: INSERT INTO TEAM
VALUES
(1, ―Team Titan‖) ,
(2, ―Team Rockers‖) ,
(3, ―Team Magnet‖) ,
(4, ―Team Hurricane‖);
5
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
g. Now create another table below. MATCH_DETAILS and insert data as shown in
table. Choose appropriate domains and constraints for each attribute.
h. Use the foreign key constraint in the MATCH_ DETAILS table with reference to
TEAM table so that MATCH_DETAILS table records score of teams existing in
the TEAM table only.
b. Retrieve the MatchID of all those matches where FirstTeam has scored < 70 but
SecondTeam has scored > 70.
Ans: SELECT MatchID FROM MATCH_DETAILS
WHERE FirstTeamScore <70 AND SecondTeamScore >70;
6
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
c. Find out the MatchID and date of matches played by Team 1 and won by it.
Ans: SELECT MatchID, MatchDate FROM MATCH_DETAILS
WHERE FirstTeamID = 1 AND FirstTeamScore > SecondTeamScore;
d. Find out the MatchID of matches played by Team 2 and not won by it.
Ans: SELECT MatchID FROM MATCH_DETAILS
WHERE FirstTeamID = 2 AND FirstTeamScore > SecondTeamScore;
e. In the TEAM relation, change the name of the relation to T_DATA. Also change
the attributes TeamID and TeamName to T_ID and T_NAME respectively.
7
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
Table: PROJECT_ASSIGNED
Table: PROJECT
8
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
Table PrimaryLey
STUDENT RegistrationID
PROJECT ProjectID
PROJECT_ASSIGNED -
f. Finds names of all the students studying in class ‗Commerce stream‘ and are
guided by same teacher, even if they are assigned different projects.
Ans:
SELECT Name FROM STUDENT, PROJECT, PROJECT_ASSGINED
WHERE (STUDENT.RegistrationID = PROJECT_ASSIGNED.RegistrationID
AND PROJECT.ProjectID = PROJECT_ASSIGNED.ProjectID) AND
(Stream = ―Commerce Stream‖ AND GuideTeacher = ―teachername‖);
9
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
10. A shop called Wonderful Garments that sells school uniforms maintain a database
SCHOOL_UNIFORM as shown below. It consisted of two relations — UNIFORM and
PRICE. They made UniformCode as the primary key for UNIFORM relation. Further,
they used UniformCode and Size as composite keys for PRICE relation. By analyzing
the database schema and database state, specify SQL queries to rectify the following
anomalies.
UNIFORM PRICE
UCode UName Ucolor UCode Size Price
1 Shirt White 1 M 500
2 Pant Grey 1 L 580
3 Skirt Grey 1 XL 620
4 Tie Blue 2 M 810
5 Socks Blue 2 L 940
6 Belt blue 2 XL 940
3 M 770
3 L 830
3 XL 910
4 S 150
4 L 170
5 S 180
5 L 210
6 M 110
6 L 140
6 XL 160
a. The PRICE relation has an attribute named Price. In order to avoid confusion,
write SQL query to change the name of the relation PRICE to COST.
10
Website: www.techtipnow.in
YouTube: www.youtube.com/c/techtipnow
b. M/S Wonderful Garments also keeps handkerchiefs of red color, medium size of
100 each. Insert this record in COST table.
Ans: INSERT INTO UNIFORM VALUES (7, ‗Handkerchief‘, ‗Red‘)
INSERT INTO COST VALUES (7, ‗M‘, 100);
c. When you used the above query to insert data, you were able to enter the values
for handkerchief without entering its details in the UNIFORM relation. Make a
provision so that the data can be entered in COST table only if it is already there
in UNIFROM table.
d. Further, you should be able to assign a new UCode to an item only if it has a
valid Uname. Write a query to add appropriate constraint to the SCHOOL_
UNIFORM database.
Ans: ALTER TABLE UNIFORM ADD UNIQUE (UName);
e. ALTER table to add the constraint that price of an item is always greater than
zero.
Ans: ALTER TABLE COST ADD CHECK (Price >100);
11