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 →Every part of this exercise runs against the real 8-row INVENTORY table already established in this chapter (§1.1) — never a hypothetical or assumed schema.
This is a code/query task. Starting data (Table 1.1, INVENTORY):
| CarId | CarName | Price | Model | YearManufacture | FuelType |
|---|---|---|---|---|---|
| D001 | Car1 | 582613.00 | LXI | 2017 | Petrol |
| D002 | Car1 | 673112.00 | VXI | 2018 | Petrol |
| B001 | Car2 | 567031.00 | Sigma1.2 | 2019 | Petrol |
| B002 | Car2 | 647858.00 | Delta1.2 | 2018 | Petrol |
| E001 | Car3 | 355205.00 | 5 STR STD | 2017 | CNG |
| E002 | Car3 | 654914.00 | CARE | 2018 | CNG |
| S001 | Car4 | 514000.00 | LXI | 2017 | Petrol |
| S002 | Car4 | 614000.00 | VXI | 2018 | Petrol |
(a) Add the column with ALTER TABLE:
ALTER TABLE INVENTORY ADD Discount Numeric(5,2);
(b) A CASE expression assigns a different discount per model in a single UPDATE:
UPDATE INVENTORY SET Discount =
CASE
WHEN Model='LXI' THEN 0
WHEN Model='VXI' THEN 10
ELSE 12
END;
Applying this to every row: LXI cars (D001, S001) get 0; VXI cars (D002, S002) get 10; everything else — Sigma1.2, Delta1.2, 5 STR STD, CARE (B001, B002, E001, E002) — gets 12:
| CarId | CarName | Model | Price | Discount |
|---|---|---|---|---|
| D001 | Car1 | LXI | 582613.00 | 0 |
| D002 | Car1 | VXI | 673112.00 | 10 |
| B001 | Car2 | Sigma1.2 | 567031.00 | 12 |
| B002 | Car2 | Delta1.2 | 647858.00 | 12 |
| E001 | Car3 | 5 STR STD | 355205.00 | 12 |
| E002 | Car3 | CARE | 654914.00 | 12 |
| S001 | Car4 | LXI | 514000.00 | 0 |
| S002 | Car4 | VXI | 614000.00 | 10 |
(c) Filter to Petrol, sort by Price descending, keep only the top row:
SELECT CarName FROM INVENTORY WHERE FuelType='Petrol' ORDER BY Price DESC LIMIT 1;
Petrol cars are D001, D002, B001, B002, S001, S002 (E001 and E002 are CNG). Their prices: 582613, 673112, 567031, 647858, 514000, 614000. The highest is D002 at 673112.00, whose CarName is "Car1". …
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.