0% found this document useful (0 votes)
2 views

Chapter 9 12th sql

The document covers various aspects of Structured Query Language (SQL) including definitions of RDBMS, SQL clauses like ORDER BY and GROUP BY, differences between functions, and operations like Cartesian Product. It also includes SQL queries related to a MOVIE table and a SPORTS database, detailing how to create tables, insert data, and retrieve information. Additionally, it provides examples of SQL statements and their expected outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 9 12th sql

The document covers various aspects of Structured Query Language (SQL) including definitions of RDBMS, SQL clauses like ORDER BY and GROUP BY, differences between functions, and operations like Cartesian Product. It also includes SQL queries related to a MOVIE table and a SPORTS database, detailing how to create tables, insert data, and retrieve information. Additionally, it provides examples of SQL statements and their expected outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Chapter 9

Structured Query Language (SQL)


Question 1(a)

Define RDBMS. Name any two RDBMS software.


Answer
An RDBMS is a database management system based on the relational model, storing
data in tables of rows and columns, enabling data retrieval, manipulation, and
relationships through SQL queries. Two examples of RDBMS software are MySQL and
Oracle Database.

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:

Single Row Functions Aggregate Functions

Single row functions operate on Aggregate functions operate on groups


individual rows and return a single of rows and return a single result for
value per row. each group.

It can be used in SELECT, WHERE, It can be used in the SELECT clause


and ORDER BY clause. only.
Question 1(d)

What do you understand by Cartesian Product?


Answer
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)

Differentiate between the following statements:


(i) ALTER and UPDATE
(ii) DELETE and DROP
Answer
(i) Differences between ALTER and UPDATE statements:

ALTER statement UPDATE statement

The ALTER statement is used to


modify the structure of database The UPDATE statement is used to
objects, such as tables, views, or modify the existing data in a table.
schemas.

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.

For example: UPDATE Employees SET


For example: ALTER TABLE Employees Email = '[email protected]' WHERE
ADD Email VARCHAR(255); EmployeeID = 101;

(ii) Differences between DELETE and DROP statements:

DELETE statement DROP statement

The DROP statement is used to remove


The DELETE statement is used to
entire database objects, such as tables,
remove one or more rows from a
views, indexes, or schemas, from the
table based on specified conditions.
database.
DELETE statement DROP statement

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)

Write the name of the functions to perform the following operations:

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

Write the output produced by the following SQL statements:


(a) SELECT POW(2, 3);
(b) SELECT ROUND(342.9234, -1);
(c) SELECT LENGTH("Informatics Practices");
(d) SELECT YEAR("1979/11/26"), MONTH("1979/11/26"), DAY("1979/11/26"),
MONTHNAME("1979/11/26");
(e) SELECT LEFT("INDIA", 3), RIGHT("ComputerScience", 4), MID("Informatics", 3, 4),
SUBSTR("Practices", 3);
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") |
+--------------------+---------------------+-------------------+--------------------
-----+
| 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.

MovieI Categor ReleaseDa ProductionC BusinessCo


MovieName
D y te ost st

001 Hindi_Movie Musical 2018-04-23 124500 130000


MovieI Categor ReleaseDa ProductionC BusinessCo
MovieName
D y te ost st

002 Tamil_Movie Action 2016-05-17 112000 118000

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

(a) Display all the information from the Movie table.


(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.
(c) List the different categories of movies.
(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 | 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:

1. It should have a column TeamID for storing an integer value between 1 to 9,


which refers to unique identification of a team.
2. Each TeamID should have its associated name (TeamName), which should be a
string of length not less than 10 characters.

(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

Match MatchD FirstTea SecondTea FirstTeamSc SecondTeamS


ID ate mID mID ore core

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

SELECT * FROM MATCH_DETAILS;


+---------+------------+-------------+--------------+----------------+--------------
---+
| MatchID | MatchDate | FirstTeamID | SecondTeamID | FirstTeamScore |
SecondTeamScore |
+---------+------------+-------------+--------------+----------------+--------------
---+
| 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 |
+---------+------------+-------------+--------------+----------------+--------------
---+

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.

PCode PName UPrice Manufacturer

P01 Washing Powder 120 Surf

P02 Toothpaste 54 Colgate

P03 Soap 25 Lux

P04 Toothpaste 65 Pepsodent

P05 Soap 38 Dove

P06 Shampoo 245 Dove

Write SQL queries for the following:


(a) Create the table Product with appropriate data types and constraints.
(b) Identify the primary key in Product.
(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.
(d) Add a new column Discount to the table Product.
(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.
(g) Display the total number of products manufactured by each 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;
Answer
(a)
CREATE TABLE Product (
PCode VARCHAR(10) PRIMARY KEY,
PName VARCHAR(50),
UPrice int,
Manufacturer VARCHAR(50)
);
(b) The primary key in the table "Product" is PCode.
(c)
SELECT PCode, PName, UPrice
FROM Product
ORDER BY PName DESC, UPrice ASC;

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

SELECT * FROM Product;

+-------+----------------+--------+--------------+----------+
| 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

SELECT * from Product;

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

1. No discount is available on the LXI model.


2. VXI model gives a 10 per cent discount.
3. A 12 per cent discount is given on cars other than LXI model and VXI model.

(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

B00 567031. Sigma1.


Baleno 2019 Petrol 635074.7
1 00 2

B00 647858. Delta1.


Baleno 2018 Petrol 725601.0
2 00 2

E00 355205. 5 STR


EECO 2017 CNG 397829.6
1 00 STD

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 |
+----------+

You might also like