Chapter 9 12th sql
Chapter 9 12th sql
Question 1(b)
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(e)
It can be used to add, modify, or drop It is used to change the values of one or
columns, constraints, or indexes in a more columns in a table based on
table. specified conditions.
It deletes specific rows of data while It deletes the entire object along with its
keeping the table structure intact. structure and data.
For example, DELETE FROM Employees For example, DROP TABLE Products;
WHERE Department = 'Marketing';
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
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") |
+--------------------+---------------------+-------------------+--------------------
-----+
| 1979 | 11 | 26 | November
|
+--------------------+---------------------+-------------------+--------------------
-----+
(e)
Output
+------------------+-----------------------------+--------------------------+-------
-----------------+
| LEFT("INDIA", 3) | RIGHT("ComputerScience", 4) | MID("Informatics", 3, 4) |
SUBSTR("Practices", 3) |
+------------------+-----------------------------+--------------------------+-------
-----------------+
| IND | ence | form |
actices |
+------------------+-----------------------------+--------------------------+-------
-----------------+
Question 3
Consider the following MOVIE table and write the SQL queries based on it.
English_Mo
003 Horror 2017-08-06 245000 360000
vie
Bengali_Mo Adventu
004 2017-01-04 72000 100000
vie re
Telugu_Mov
005 Action - 100000 -
ie
Punjabi_Mo
006 Comedy - 30500 -
vie
Output
+---------+---------------+-----------+-------------+----------------+--------------
+
| MOVIEID | MOVIENAME | CATEGORY | RELEASEDATE | PRODUCTIONCOST | BUSINESSCOST
|
+---------+---------------+-----------+-------------+----------------+--------------
+
| 1 | Hindi_Movie | Musical | 2018-04-23 | 124500 | 130000
|
| 2 | Tamil_Movie | Action | 2016-05-17 | 112000 | 118000
|
| 3 | English_Movie | Horror | 2017-08-06 | 245000 | 360000
|
| 4 | Bengali_Movie | Adventure | 2017-01-04 | 72000 | 100000
|
| 5 | Telugu_Movie | Action | NULL | 100000 | NULL
|
| 6 | Punjabi_Movie | Comedy | NULL | 30500 | NULL
|
+---------+---------------+-----------+-------------+----------------+--------------
+
(b)
SELECT MovieID, MovieName, (ProductionCost + BusinessCost) AS Total_Earning
FROM Movie
WHERE ReleaseDate IS NOT NULL;
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)
SELECT MovieID, MovieName, BusinessCost - ProductionCost AS NetProfit
FROM Movie
WHERE ReleaseDate IS NOT NULL;
Output
+---------+---------------+-----------+
| MovieID | MovieName | NetProfit |
+---------+---------------+-----------+
| 1 | Hindi_Movie | 5500 |
| 2 | Tamil_Movie | 6000 |
| 3 | English_Movie | 115000 |
| 4 | Bengali_Movie | 28000 |
+---------+---------------+-----------+
(e)
SELECT MovieID, MovieName, ProductionCost AS Cost
FROM MOVIE
WHERE ProductionCost > 10000 AND ProductionCost < 100000;
Output
+---------+---------------+-------+
| MovieID | MovieName | Cost |
+---------+---------------+-------+
| 4 | Bengali_Movie | 72000 |
| 6 | Punjabi_Movie | 30500 |
+---------+---------------+-------+
(f)
SELECT * FROM MOVIE
WHERE Category = 'Comedy' OR Category = 'Action';
Output
+---------+---------------+----------+-------------+----------------+--------------+
| MOVIEID | MOVIENAME | CATEGORY | RELEASEDATE | PRODUCTIONCOST | BUSINESSCOST |
+---------+---------------+----------+-------------+----------------+--------------+
| 2 | Tamil_Movie | Action | 2016-05-17 | 112000 | 118000 |
| 5 | Telugu_Movie | Action | NULL | 100000 | NULL |
| 6 | Punjabi_Movie | Comedy | NULL | 30500 | NULL |
+---------+---------------+----------+-------------+----------------+--------------+
(g)
SELECT * FROM MOVIE
WHERE ReleaseDate IS NULL;
Output
+---------+---------------+----------+-------------+----------------+--------------+
| MOVIEID | MOVIENAME | CATEGORY | RELEASEDATE | PRODUCTIONCOST | BUSINESSCOST |
+---------+---------------+----------+-------------+----------------+--------------+
| 5 | Telugu_Movie | Action | NULL | 100000 | NULL |
| 6 | Punjabi_Movie | Comedy | NULL | 30500 | NULL |
+---------+---------------+----------+-------------+----------------+--------------+
Question 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".
(b) Create a table "TEAM" with following considerations:
(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:
Row 1: (1, Team Titan)
Row 2: (2, Team Rockers)
Row 3: (3, Team Magnet)
Row 4: (4, Team Hurricane)
(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 86
17
2018-07-
M2 3 4 45 48
18
2018-07-
M3 1 3 78 56
19
2018-07-
M4 2 4 56 67
19
Match MatchD FirstTea SecondTea FirstTeamSc SecondTeamS
ID ate mID mID ore core
2018-07-
M5 1 4 32 87
18
2018-07-
M6 2 3 67 51
17
Answer
(a)
CREATE DATABASE Sports;
(b)
CREATE TABLE TEAM (
TeamID INT,
TeamName VARCHAR(20));
(c)
ALTER TABLE TEAM
ADD PRIMARY KEY(TeamID);
(d)
DESCRIBE TEAM;
Output
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| TeamID | int | NO | PRI | NULL | |
| TeamName | char(20) | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
(e)
INSERT INTO TEAM (TeamID, TeamName) VALUES
(1, 'Team Titan'),
(2, 'Team Rockers'),
(3, 'Team Magnet'),
(4, 'Team Hurricane');
(f)
SELECT * FROM TEAM;
Output
+--------+----------------+
| TeamID | TeamName |
+--------+----------------+
| 1 | Team Titan |
| 2 | Team Rockers |
| 3 | Team Magnet |
| 4 | Team Hurricane |
+--------+----------------+
(g)
CREATE TABLE MATCH_DETAILS (
MatchID VARCHAR(10),
MatchDate DATE,
FirstTeamID INT,
SecondTeamID INT,
FirstTeamScore INT,
SecondTeamScore INT,
CONSTRAINT PK_MatchID PRIMARY KEY (MatchID),
);
INSERT INTO MATCH_DETAILS (MatchID, MatchDate, FirstTeamID, SecondTeamID,
FirstTeamScore, SecondTeamScore) VALUES
('M1', '2018-07-17', 1, 2, 90, 86),
('M2', '2018-07-18', 3, 4, 45, 48),
('M3', '2018-07-19', 1, 3, 78, 56),
('M4', '2018-07-19', 2, 4, 56, 67),
('M5', '2018-07-18', 1, 4, 32, 87),
('M6', '2018-07-17', 2, 3, 67, 51);
Output
Question 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.
(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)
SELECT MatchID, MatchDate
FROM MATCH_DETAILS
WHERE FirstTeamID = 1 AND FirstTeamScore > SecondTeamScore;
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)
ALTER TABLE TEAM RENAME TO T_DATA;
ALTER TABLE T_DATA CHANGE COLUMN TeamID T_ID int;
ALTER TABLE T_DATA CHANGE COLUMN TeamName T_NAME CHAR(20);
Question 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
analysing 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.
(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)
ALTER TABLE COST ADD CONSTRAINT fk_uniform_ucode_size FOREIGN KEY (UCode) REFERENCES
UNIFORM (UCode);
(c)
ALTER TABLE UNIFORM ADD CONSTRAINT CK_UName_UCode
CHECK (UName IS NOT NULL);
(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.
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)
SELECT Manufacturer, COUNT(*) AS TotalProducts
FROM Product
GROUP BY Manufacturer;
Output
+--------------+---------------+
| Manufacturer | TotalProducts |
+--------------+---------------+
| SURF | 1 |
| COLGATE | 1 |
| LUX | 1 |
| PEPSODENT | 1 |
| DOVE | 2 |
+--------------+---------------+
(h)
SELECT PName, avg(UPrice) FROM Product GROUP
BY Pname;
Output
+----------------+-------------+
| PName | avg(UPrice) |
+----------------+-------------+
| WASHING POWDER | 120.0000 |
| TOOTHPASTE | 59.5000 |
| SOAP | 34.0000 |
| SHAMPOO | 274.0000 |
+----------------+-------------+
(i)
SELECT DISTINCT Manufacturer FROM Product;
Output
+--------------+
| Manufacturer |
+--------------+
| SURF |
| COLGATE |
| LUX |
| PEPSODENT |
| DOVE |
+--------------+
(j)
SELECT COUNT(DISTINCT PName) FROM Product;
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:
(a) Add a new column Discount in the INVENTORY table.
(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.
(e) List the total number of cars having no discount.
Answer
Table inventory
CarI CarNam YearManufact FuelTyp FinalPri
Price Model
d e ure e ce
D00 582613.
Dzire LXI 2017 Petrol 652526.6
1 00
D00 673112.
Dzire VXI 2018 Petrol 753885.4
2 00
E00 654914.
EECO CARE 2018 CNG 733503.7
2 00
S00 514000.
SWIFT LXI 2017 Petrol 575680.0
1 00
S00 614000.
SWIFT VXI 2018 Petrol 687680.0
2 00
(a)
ALTER TABLE INVENTORY
ADD COLUMN Discount FLOAT;
(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 | FinalPrice
| Discount |
+-------+---------+-----------+-----------+-----------------+----------+------------
+----------+
| D001 | Dzire | 582613.00 | LXI | 2017 | Petrol | 652526.60
| 0 |
| D002 | Dzire | 673112.00 | VXI | 2018 | Petrol | 753885.40
| 67311.2 |
| B001 | Baleno | 567031.00 | Sigma1.2 | 2019 | Petrol | 635074.70
| 68043.7 |
| B002 | Baleno | 647858.00 | Delta1.2 | 2018 | Petrol | 725601.00
| 77743 |
| E001 | EECO | 355205.00 | 5 STR STD | 2017 | CNG | 397829.60
| 42624.6 |
| E002 | EECO | 654914.00 | CARE | 2018 | CNG | 733503.70
| 78589.7 |
| S001 | SWIFT | 514000.00 | LXI | 2017 | Petrol | 575680.00
| 0 |
| S002 | SWIFT | 614000.00 | VXI | 2018 | Petrol | 687680.00
| 61400 |
+-------+---------+-----------+-----------+-----------------+----------+------------
+----------+
(c)
SELECT CarName
FROM INVENTORY
WHERE FuelType = 'Petrol'
AND Price = (SELECT MAX(Price) FROM INVENTORY WHERE FuelType = 'Petrol');
Output
+---------+
| CarName |
+---------+
| Dzire |
+---------+
(d)
SELECT AVG(Discount) AS AverageDiscount,
SUM(Discount) AS TotalDiscount
FROM INVENTORY
WHERE CarName = 'Baleno';
Output
+-----------------+----------------+
| AverageDiscount | TotalDiscount |
+-----------------+----------------+
| 72893.33984375 | 145786.6796875 |
+-----------------+----------------+
(e)
SELECT COUNT(*)
FROM INVENTORY
WHERE Discount = 0;
Output
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+