Chapter 9 - Structured Query Language (SQL) - NCERT Solutions For Class 12 Computer Science Code 083 CB
Chapter 9 - Structured Query Language (SQL) - NCERT Solutions For Class 12 Computer Science Code 083 CB
Home / Class 12 - NCERT Computer Science Solutions / Structured Query Language (SQL)
Chapter 9
Structured Query Language (SQL)
Class 12 - NCERT Computer Science Solutions
Exercise
Question 1(a)
Answer
Question 1(b)
What is the purpose of the following clauses in a select statement?
(i) ORDER BY
(ii) GROUP BY
Answer
(i) ORDER BY clause is used to sort the result set of a SELECT statement either
in ascending (default) or descending order based on one or more columns. The
ASC keyword is used for ascending order, and the DESC keyword is used for
descending order.
(ii) GROUP BY clause is used to group rows that have the same values in
specified columns into summary rows. It is commonly used with aggregate
functions (e.g., SUM, COUNT, AVG) to perform calculations on grouped data.
Question 1(c)
Site any two differences between Single Row Functions and Aggregate Functions.
Answer
Two differences between Single Row Functions and Aggregate Functions are:
Question 1(d)
The Cartesian Product is an operation that combines tuples from two relations. It
results in all possible pairs of rows from the two input relations, regardless of
whether they have matching values on common attributes. This operation is
denoted by the cross join symbol (×) in SQL.
Question 1(e)
Answer
Chapter 5 CONTENTS
Sorting (ii) Differences between DELETE and DROP statements:
Exercise
Chapter 6
Chapter 6
Searching
DELETE statement DROP statement
Chapter 7
The DELETE statement is used to The DROP statement is used to
Understanding Data
remove one or more rows from a remove entire database objects, such
Chapter 8
table based on specified as tables, views, indexes, or schemas,
Database Concepts
conditions. from the database.
Chapter 9
Structured Query Language (SQL) It deletes specific rows of data
It deletes the entire object along with its
Chapter 10
while keeping the table structure
structure and data.
Computer Networks intact.
Question 1(f)
1. To display the day like 'Monday', 'Tuesday', from the date when India got
independence.
2. To display the specified number of characters from a particular position of the
given string.
3. To display the name of the month in which you were born.
4. To display your name in capital letters.
Answer
1. DAYNAME("1947-08-15")
2. SUBSTRING(string, pos, n)
3. MONTHNAME("yyyy-mm-dd")
4. UPPER('YourName')
Question 2
Answer
(a)
Output
+-----------+
| POW(2, 3) |
+-----------+
| 8 |
+-----------+
(b)
Output
+---------------------+
| ROUND(342.9234, -1) |
+---------------------+
| 340 |
+---------------------+
(c)
Output
+---------------------------------+
| LENGTH("Informatics Practices") |
+---------------------------------+
| 21 |
+---------------------------------+
(d)
Output
+--------------------+---------------------+-------------------+-----------
| YEAR("1979/11/26") | MONTH("1979/11/26") | DAY("1979/11/26") | MONTHNAME(
+--------------------+---------------------+-------------------+-----------
| 1979 | 11 | 26 | November
+--------------------+---------------------+-------------------+-----------
(e)
Output
+------------------+-----------------------------+-------------------------
| LEFT("INDIA", 3) | RIGHT("ComputerScience", 4) | MID("Informatics", 3, 4)
+------------------+-----------------------------+-------------------------
| IND | ence | form
+------------------+-----------------------------+-------------------------
Question 3
Consider the following MOVIE table and write the SQL queries based on it.
MovieID MovieName Category ReleaseDate ProductionCost
(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.
(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.
(e) List MovieID, MovieName and Cost for all movies with ProductionCost greater
than 10,000 and less than 1,00,000.
(f) List details of all movies which fall in the category of comedy or action.
(g) List details of all movies which have not been released yet.
Answer
(a)
SELECT * FROM Movie;
Output
+---------+---------------+-----------+-------------+----------------+-----
| MOVIEID | MOVIENAME | CATEGORY | RELEASEDATE | PRODUCTIONCOST | BUSI
+---------+---------------+-----------+-------------+----------------+-----
| 1 | Hindi_Movie | Musical | 2018-04-23 | 124500 |
| 2 | Tamil_Movie | Action | 2016-05-17 | 112000 |
| 3 | English_Movie | Horror | 2017-08-06 | 245000 |
| 4 | Bengali_Movie | Adventure | 2017-01-04 | 72000 |
| 5 | Telugu_Movie | Action | NULL | 100000 |
| 6 | Punjabi_Movie | Comedy | NULL | 30500 |
+---------+---------------+-----------+-------------+----------------+-----
(b)
Output
+---------+---------------+---------------+
| MovieID | MovieName | Total_Earning |
+---------+---------------+---------------+
| 1 | Hindi_Movie | 254500 |
| 2 | Tamil_Movie | 230000 |
| 3 | English_Movie | 605000 |
| 4 | Bengali_Movie | 172000 |
+---------+---------------+---------------+
(c)
SELECT DISTINCT Category FROM MOVIE;
Output
+-----------+
| Category |
+-----------+
| Musical |
| Action |
| Horror |
| Adventure |
| Comedy |
+-----------+
(d)
Output
+---------+---------------+-----------+
| MovieID | MovieName | NetProfit |
+---------+---------------+-----------+
| 1 | Hindi_Movie | 5500 |
| 2 | Tamil_Movie | 6000 |
| 3 | English_Movie | 115000 |
| 4 | Bengali_Movie | 28000 |
+---------+---------------+-----------+
(e)
+---------+---------------+-------+
| MovieID | MovieName | Cost |
+---------+---------------+-------+
| 4 | Bengali_Movie | 72000 |
| 6 | Punjabi_Movie | 30500 |
+---------+---------------+-------+
(f)
Output
+---------+---------------+----------+-------------+----------------+------
| MOVIEID | MOVIENAME | CATEGORY | RELEASEDATE | PRODUCTIONCOST | BUSIN
+---------+---------------+----------+-------------+----------------+------
| 2 | Tamil_Movie | Action | 2016-05-17 | 112000 |
| 5 | Telugu_Movie | Action | NULL | 100000 |
| 6 | Punjabi_Movie | Comedy | NULL | 30500 |
+---------+---------------+----------+-------------+----------------+------
(g)
Output
+---------+---------------+----------+-------------+----------------+------
| MOVIEID | MOVIENAME | CATEGORY | RELEASEDATE | PRODUCTIONCOST | BUSIN
+---------+---------------+----------+-------------+----------------+------
| 5 | Telugu_Movie | Action | NULL | 100000 |
| 6 | Punjabi_Movie | Comedy | NULL | 30500 |
+---------+---------------+----------+-------------+----------------+------
Question 4
(c) Using table level constraint, make TeamID as the primary key.
(d) Show the structure of the table TEAM using a SQL statement.
(e) As per the preferences of the students four teams were formed as given
below. Insert these four rows in TEAM table:
(f) Show the contents of the table TEAM using a DML statement.
(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
2018-07-
M1 1 2 90
17
2018-07-
M2 3 4 45
18
2018-07-
M3 1 3 78
19
2018-07-
M4 2 4 56
19
2018-07-
M5 1 4 32
18
2018-07-
M6 2 3 67
17
Answer
(a)
(b)
(c)
(d)
DESCRIBE TEAM;
Output
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| TeamID | int | NO | PRI | NULL | |
| TeamName | char(20) | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
(e)
(f)
Output
+--------+----------------+
| TeamID | TeamName |
+--------+----------------+
| 1 | Team Titan |
| 2 | Team Rockers |
| 3 | Team Magnet |
| 4 | Team Hurricane |
+--------+----------------+
(g)
Output
+---------+------------+-------------+--------------+----------------+-----
| MatchID | MatchDate | FirstTeamID | SecondTeamID | FirstTeamScore | Seco
+---------+------------+-------------+--------------+----------------+-----
| M1 | 2018-07-17 | 1 | 2 | 90 |
| M2 | 2018-07-18 | 3 | 4 | 45 |
| M3 | 2018-07-19 | 1 | 3 | 78 |
| M4 | 2018-07-19 | 2 | 4 | 56 |
| M5 | 2018-07-18 | 1 | 4 | 32 |
| M6 | 2018-07-17 | 2 | 3 | 67 |
+---------+------------+-------------+--------------+----------------+-----
Question 5
(a) Display the MatchID of all those matches where both the teams have scored
more than 70.
(b) Display the MatchID of all those matches where FirstTeam has scored less
than 70 but SecondTeam has scored more than 70.
(c) Display the MatchID and date of matches played by Team 1 and won by it.
(d) Display the MatchID of matches played by Team 2 and not won by it.
(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
(a)
SELECT MatchID
FROM MATCH_DETAILS
WHERE FirstTeamScore > 70 AND SecondTeamScore > 70;
Output
+---------+
| MatchID |
+---------+
| M1 |
+---------+
(b)
SELECT MatchID
FROM MATCH_DETAILS
WHERE FirstTeamScore < 70 AND SecondTeamScore > 70;
Output
+---------+
| MatchID |
+---------+
| M5 |
+---------+
(c)
Output
+---------+------------+
| MatchID | MatchDate |
+---------+------------+
| M1 | 2018-07-17 |
| M3 | 2018-07-19 |
+---------+------------+
(d)
SELECT MatchID
FROM MATCH_DETAILS
WHERE SecondTeamID = 2 AND SecondTeamScore <= FirstTeamScore;
Output
+---------+
| MatchID |
+---------+
| M1 |
+---------+
(e)
Question 6
(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.
(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.
(d) Add the constraint so that the price of an item is always greater than zero.
Answer
(b)
(c)
(d)
ALTER TABLE COST ADD CONSTRAINT CK_Price_Positive CHECK (Price > 0);
Question 7
Consider the following table named "Product", showing details of products being
sold in a grocery shop.
(a) Create the table Product with appropriate data types and constraints.
(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.
(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.
(f) Increase the price by 12 per cent for all the products manufactured by Dove.
Write the output(s) produced by executing the following queries on the basis of the
information given above in the table Product:
Answer
(a)
(c)
Output
+-------+----------------+--------+
| PCode | PName | UPrice |
+-------+----------------+--------+
| P01 | WASHING POWDER | 120 |
| P02 | TOOTHPASTE | 54 |
| P04 | TOOTHPASTE | 65 |
| P03 | SOAP | 25 |
| P05 | SOAP | 38 |
| P06 | SHAMPOO | 245 |
+-------+----------------+--------+
(d)
ALTER TABLE Product
ADD COLUMN Discount float;
(e)
UPDATE Product
SET Discount = IF(UPrice > 100, (UPrice * (10/100)) + UPrice, 0);
Output
+-------+----------------+--------+--------------+----------+
| PCode | PName | UPrice | Manufacturer | Discount |
+-------+----------------+--------+--------------+----------+
| P01 | WASHING POWDER | 120 | SURF | 12 |
| P02 | TOOTHPASTE | 54 | COLGATE | 0 |
| P03 | SOAP | 25 | LUX | 0 |
| P04 | TOOTHPASTE | 65 | PEPSODENT | 0 |
| P05 | SOAP | 38 | DOVE | 0 |
| P06 | SHAMPOO | 245 | DOVE | 24.5 |
+-------+----------------+--------+--------------+----------+
(f)
UPDATE Product
SET UPrice = (UPrice * (12/100)) + UPrice
WHERE Manufacturer = 'Dove';
Output
+-------+----------------+--------+--------------+----------+
| PCode | PName | UPrice | Manufacturer | Discount |
+-------+----------------+--------+--------------+----------+
| P01 | WASHING POWDER | 120 | SURF | 12 |
| P02 | TOOTHPASTE | 54 | COLGATE | 0 |
| P03 | SOAP | 25 | LUX | 0 |
| P04 | TOOTHPASTE | 65 | PEPSODENT | 0 |
| P05 | SOAP | 43 | DOVE | 0 |
| P06 | SHAMPOO | 274 | DOVE | 24.5 |
+-------+----------------+--------+--------------+----------+
(g)
Output
+--------------+---------------+
| Manufacturer | TotalProducts |
+--------------+---------------+
| SURF | 1 |
| COLGATE | 1 |
| LUX | 1 |
| PEPSODENT | 1 |
| DOVE | 2 |
+--------------+---------------+
(h)
Output
+----------------+-------------+
| PName | avg(UPrice) |
+----------------+-------------+
| WASHING POWDER | 120.0000 |
| TOOTHPASTE | 59.5000 |
| SOAP | 34.0000 |
| SHAMPOO | 274.0000 |
+----------------+-------------+
(i)
Output
+--------------+
| Manufacturer |
+--------------+
| SURF |
| COLGATE |
| LUX |
| PEPSODENT |
| DOVE |
+--------------+
(j)
Output
+-----------------------+
| COUNT(DISTINCT PName) |
+-----------------------+
| 4 |
+-----------------------+
(k)
SELECT PName, MAX(UPrice), MIN(UPrice) FROM
Product GROUP BY PName;
Output
+----------------+-------------+-------------+
| PName | MAX(UPrice) | MIN(UPrice) |
+----------------+-------------+-------------+
| WASHING POWDER | 120 | 120 |
| TOOTHPASTE | 65 | 54 |
| SOAP | 43 | 25 |
| SHAMPOO | 274 | 274 |
+----------------+-------------+-------------+
Question 8
Using the CARSHOWROOM database given in the chapter, write the SQL queries
for the following:
(b) Set appropriate discount values for all cars keeping in mind the following:
(c) Display the name of the costliest car with fuel type "Petrol".
(d) Calculate the average discount and total discount available on Baleno cars.
Answer
Table inventory
CarId CarName Price Model YearManufacture FuelType
5 STR
E001 EECO 355205.00 2017 CNG
STD
(a)
(b)
UPDATE INVENTORY
SET Discount = 0
WHERE Model = 'LXI';
UPDATE INVENTORY
SET Discount = Price * 0.10
WHERE Model = 'VXI';
UPDATE INVENTORY
SET Discount = Price * 0.12
WHERE Model NOT IN ('LXI', 'VXI');
Output
+-------+---------+-----------+-----------+-----------------+----------+---
| CarId | CarName | Price | Model | YearManufacture | FuelType | Fi
+-------+---------+-----------+-----------+-----------------+----------+---
| D001 | Dzire | 582613.00 | LXI | 2017 | Petrol | 6
| D002 | Dzire | 673112.00 | VXI | 2018 | Petrol | 7
| B001 | Baleno | 567031.00 | Sigma1.2 | 2019 | Petrol | 6
| B002 | Baleno | 647858.00 | Delta1.2 | 2018 | Petrol | 7
| E001 | EECO | 355205.00 | 5 STR STD | 2017 | CNG | 39
| E002 | EECO | 654914.00 | CARE | 2018 | CNG | 7
| S001 | SWIFT | 514000.00 | LXI | 2017 | Petrol | 5
| S002 | SWIFT | 614000.00 | VXI | 2018 | Petrol | 6
+-------+---------+-----------+-----------+-----------------+----------+---
(c)
SELECT CarName
FROM INVENTORY
WHERE FuelType = 'Petrol'
AND Price = (SELECT MAX(Price) FROM INVENTORY WHERE FuelType = 'Petrol');
Output
+---------+
| CarName |
+---------+
| Dzire |
+---------+
(d)
+-----------------+----------------+
| AverageDiscount | TotalDiscount |
+-----------------+----------------+
| 72893.33984375 | 145786.6796875 |
+-----------------+----------------+
(e)
SELECT COUNT(*)
FROM INVENTORY
WHERE Discount = 0;
Output
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+
Class - 6 Effective History & Civics Solutions Java Number Programs (ICSE Classes 9 / 10) Contact Us
Class - 7 Concise Physics Selina Solutions Java Number Programs (ISC Classes 11 / 12) Privacy Policy
Class - 7 Concise Chemistry Selina Solutions Output Questions for Class 10 ICSE Computer Applications Terms of Service
Class - 7 Dalal Simplified Middle School Chemistry Solutions Algorithms & Flowcharts for ICSE Computers
Class - 7 Concise Biology Selina Solutions ICSE Class 8 Computers Differentiate Between the Following
Class - 7 Living Science Biology Ratna Sagar Solutions CBSE TEXTBOOK SOLUTIONS
Class - 7 Effective History & Civics Solutions Class - 9 NCERT Mathematics Solutions
Class - 8 Concise Chemistry Selina Solutions Class - 9 NCERT Geography Contemporary India 1 Solutions
Class - 8 Dalal Simplified Middle School Chemistry Solutions Class - 9 NCERT History India & Contemporary World 1 Solutions
Class - 8 Concise Biology Selina Solutions Class - 9 Sumita Arora Computer Code 165 Solutions
Class - 8 Living Science Biology Ratna Sagar Solutions Class - 9 Kips Cyber Beans Computer Code 165 Solutions
Class - 8 Around the World Geography Solutions Class - 10 NCERT Mathematics Solutions
Class - 8 Effective History & Civics Solutions Class - 10 NCERT Geography Contemporary India 2 Solutions
Class - 8 Kips Logix Computers Solutions Class - 10 NCERT History India & Contemporary World 2 Solutions
Class - 9 Concise Physics Selina Solutions Class - 10 NCERT Democratic Politics 2 (Civics) Solutions
Class - 9 Concise Chemistry Selina Solutions Class - 10 NCERT Economic Development Solutions
Class - 9 Dalal Simplified ICSE Chemistry Solutions Class - 10 Sumita Arora Computer Code 165 Solutions
Class - 9 Concise Biology Selina Solutions Class - 10 Kips Cyber Beans Computer Code 165 Solutions
Class - 9 Total Geography Morning Star Solutions Class - 11 CBSE Sumita Arora Python Solutions
Class - 9 Veena Bhargava Geography Solutions Class - 11 CBSE Preeti Arora Python Solutions
Class - 9 Total History & Civics Solutions Class - 11 CBSE Informatics Practices Preeti Arora Solutions
Class - 9 Kips Logix Computers Solutions Class - 12 CBSE Sumita Arora Python Solutions
Class - 10 Concise Physics Selina Solutions Class - 12 CBSE Preeti Arora Python Solutions
Class - 10 Concise Chemistry Selina Solutions Class - 12 NCERT Computer Science Solutions
Class - 10 Dalal Simplified ICSE Chemistry Solutions Class - 12 CBSE Informatics Practices Sumita Arora Solutions
Class - 10 Concise Biology Selina Solutions Class - 12 CBSE Informatics Practices Preeti Arora Solutions