Using the CARSHOWROOM database given in the chapter, write the SQL queries for the following: Display the name of the costliest car with fuel type “Petrol”.
SELECT CarName FROM INVENTORY WHERE FuelType = 'Petrol' AND Price = (SELECT MAX(Price) FROM INVENTORY WHERE FuelType = 'Petrol');
Explanation
This query displays the name of the costliest car with fuel type 'Petrol' from the INVENTORY table. The query uses a subquery to find the maximum price among all Petrol cars, then matches it with the main query to retrieve the corresponding CarName. The INVENTORY table contains attributes like CarName, Price, and FuelType as per the CARSHOWROOM database schema shown in Figure 9.2.
Solution Steps
Step 1: Identify the table (INVENTORY) and required columns (CarName, Price, FuelType)
Step 2: Filter records where FuelType = 'Petrol'
Step 3: Find the maximum price among Petrol cars using MAX() aggregate function in a subquery
Step 4: Match the maximum price with the main query to display the CarName