Q.Using the CARSHOWROOM database given in the chapter, write the SQL queries for the following:
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.
| CarId | CarName | Price | Model | YearManufacture | FuelType |
|---|---|---|---|---|---|
| D001 | Dzire | 582613.00 | LXI | 2017 | Petrol |
| D002 | Dzire | 673112.00 | VXI | 2018 | Petrol |
| B001 | Baleno | 567031.00 | Sigma1.2 | 2019 | Petrol |
| B002 | Baleno | 647858.00 | Delta1.2 | 2018 | Petrol |
| E001 | EECO | 355205.00 | 5 STR STD | 2017 | CNG |
| E002 | EECO | 654914.00 | CARE | 2018 | CNG |
| S001 | SWIFT | 514000.00 | LXI | 2017 | Petrol |
| S002 | SWIFT | 614000.00 | VXI | 2018 | Petrol |
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
- No discount on LXI.
- VXI gets 10%.
- 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:
| CarId | CarName | Price | Model | Discount |
|---|---|---|---|---|
| D001 | Dzire | 582613.00 | LXI | 0.00 |
| D002 | Dzire | 673112.00 | VXI | 67311.20 |
| B001 | Baleno | 567031.00 | Sigma1.2 | 68043.72 |
| B002 | Baleno | 647858.00 | Delta1.2 | 77742.96 |
| E001 | EECO | 355205.00 | 5 STR STD | 42624.60 |
| E002 | EECO | 654914.00 | CARE | 78589.68 |
| S001 | SWIFT | 514000.00 | LXI | 0.00 |
| S002 | SWIFT | 614000.00 | VXI | 61400.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.