Skip to content
Exercises · Q8

Q.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:
(i) No discount is available on the LXI model.
(ii) VXI model gives a 10 per cent discount.
(iii) 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.
Uttar Pradesh UpmspTextbookSubjective· 4mImportance★★★★★
51% · 48/95 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This solution uses the real INVENTORY table of the CARSHOWROOM database, given earlier in this chapter as Table 9.9 (Section 9.8). It demonstrates ALTER TABLE to add a column, UPDATE with CASE for conditional discount logic, and SELECT with aggregate functions and WHERE/ORDER BY/LIMIT to retrieve the required information.

The real INVENTORY table (Table 9.9, Section 9.8)

CarName is already a column of INVENTORY — the chapter never introduces a separate CARS table, so no join is needed for any part of this question.

CarIdCarNamePriceModelYearManufactureFuelType
D001Dzire582613.00LXI2017Petrol
D002Dzire673112.00VXI2018Petrol
B001Baleno567031.00Sigma1.22019Petrol
B002Baleno647858.00Delta1.22018Petrol
E001EECO355205.005 STR STD2017CNG
E002EECO654914.00CARE2018CNG
S001SWIFT514000.00LXI2017Petrol
S002SWIFT614000.00VXI2018Petrol

a) Add a new column Discount in the INVENTORY table.

ALTER TABLE INVENTORY
ADD Discount DECIMAL(10,2);

DECIMAL(10,2) stores the discount as a rupee amount (matching Price's own type), initially NULL for every row.


b) Set appropriate discount values for all cars

  1. No discount on LXI.
  2. VXI gets 10%.
  3. 12% on every other model (Sigma1.2, Delta1.2, 5 STR STD, CARE).
UPDATE INVENTORY
SET Discount = CASE
    WHEN Model = 'LXI' THEN 0
    WHEN Model = 'VXI' THEN Price * 0.10
    ELSE Price * 0.12
END;

State of INVENTORY after the update:

CarIdCarNamePriceModelDiscount
D001Dzire582613.00LXI0.00
D002Dzire673112.00VXI67311.20
B001Baleno567031.00Sigma1.268043.72
B002Baleno647858.00Delta1.277742.96
E001EECO355205.005 STR STD42624.60
E002EECO654914.00CARE78589.68
S001SWIFT514000.00LXI0.00
S002SWIFT614000.00VXI61400.00

c) Display the name of the costliest car with fuel type "Petrol".

SELECT CarName
FROM INVENTORY
WHERE FuelType = 'Petrol'
ORDER BY Price DESC
LIMIT 1;

Among the six Petrol-fuelled rows (D001 582613, D002 673112, B001 567031, B002 647858, S001 514000, S002 614000 — the two EECO rows are CNG and are excluded), the highest price is D002 at 673112.00.

Output

CarName
Dzire

d) Calculate the average discount and total discount available on Baleno cars.

SELECT AVG(Discount) AS AverageDiscount, SUM(Discount) AS TotalDiscount …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.