Computer Science
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:
- No discount is available on the LXI model.
- VXI model gives a 10 per cent discount.
- 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
CarId | CarName | Price | Model | YearManufacture | FuelType | FinalPrice |
---|---|---|---|---|---|---|
D001 | Dzire | 582613.00 | LXI | 2017 | Petrol | 652526.6 |
D002 | Dzire | 673112.00 | VXI | 2018 | Petrol | 753885.4 |
B001 | Baleno | 567031.00 | Sigma1.2 | 2019 | Petrol | 635074.7 |
B002 | Baleno | 647858.00 | Delta1.2 | 2018 | Petrol | 725601.0 |
E001 | EECO | 355205.00 | 5 STR STD | 2017 | CNG | 397829.6 |
E002 | EECO | 654914.00 | CARE | 2018 | CNG | 733503.7 |
S001 | SWIFT | 514000.00 | LXI | 2017 | Petrol | 575680.0 |
S002 | SWIFT | 614000.00 | VXI | 2018 | Petrol | 687680.0 |
(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 |
+----------+
Related Questions
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:
- It should have a column TeamID for storing an integer value between 1 to 9, which refers to unique identification of a team.
- 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
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 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 TDATA. Also change the attributes TeamID and TeamName to TID and T_NAME respectively.
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.
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;