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;
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 question tests SQL DDL (CREATE TABLE with constraints), DML (INSERT, UPDATE, ALTER), and querying (GROUP BY, ORDER BY, aggregate functions, DISTINCT). The primary key is PCode because it uniquely identifies each product row.
This is a mixed-format question — part SQL query writing (a–g) and part output prediction (h–k). I'll answer each sub-part in order, with full reasoning.
(a) Create the table Product with appropriate data types and constraints.
The table has four columns: PCode (text, primary key), PName (text), UPrice (numeric, likely decimal), and Manufacturer (text). The primary key constraint ensures uniqueness and non-null values.
CREATE TABLE Product (
PCode VARCHAR(3) PRIMARY KEY,
PName VARCHAR(30) NOT NULL,
UPrice DECIMAL(10,2) NOT NULL,
Manufacturer VARCHAR(30) NOT NULL
);
Use VARCHAR(3) for PCode because all codes are exactly 3 characters (P01, P02, etc.). DECIMAL(10,2) handles prices with two decimal places.
(b) Identify the primary key in Product.
The primary key is PCode. Each product has a unique code (P01 through P06), and no two rows share the same PCode. This is the natural candidate because it uniquely identifies every row.
PName is not a primary key — "Soap" and "Toothpaste" appear twice. Manufacturer is also not unique (Dove appears twice).
(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.
The ORDER BY clause handles this: first sort by PName descending, then by UPrice ascending as a tiebreaker.
SELECT PCode, PName, UPrice
FROM Product
ORDER BY PName DESC, UPrice ASC;
Expected output:
| PCode | PName | UPrice |
|---|---|---|
| P06 | Shampoo | 245.00 |
| P03 | Soap | 25.00 |
| P05 | Soap | 38.00 |
| P02 | Toothpaste | 54.00 |
| P04 | Toothpaste | 65.00 |
| P01 | Washing Powder | 120.00 |
Notice: "Soap" rows appear in ascending price order (25 then 38), and "Toothpaste" rows similarly (54 then 65).
(d) Add a new column Discount to the table Product.
Use ALTER TABLE with ADD:
ALTER TABLE Product
ADD Discount DECIMAL(10,2);
This adds a nullable column. Initially all values are NULL.
(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.
Use an UPDATE with a CASE expression:
UPDATE Product
SET Discount = CASE
WHEN UPrice > 100 THEN UPrice * 0.10
ELSE 0
END;
After update, the table becomes:
| PCode | PName | UPrice | Manufacturer | Discount |
|---|---|---|---|---|
| P01 | Washing Powder | 120.00 | Surf | 12.00 |
| P02 | Toothpaste | 54.00 | Colgate | 0.00 |
| P03 | Soap | 25.00 | Lux | 0.00 |
| P04 | Toothpaste | 65.00 | Pepsodent | 0.00 |
| P05 | Soap | 38.00 | Dove | 0.00 |
| P06 | Shampoo | 245.00 | Dove | 24.50 |
Only P01 (UPrice 120) and P06 (UPrice 245) get non-zero discounts.
(f) Increase the price by 12 per cent for all the products manufactured by Dove.
UPDATE Product
SET UPrice = UPrice * 1.12
WHERE Manufacturer = 'Dove';
After update, Dove products change:
| PCode | PName | UPrice (before) | UPrice (after) |
|---|---|---|---|
| P05 | Soap | 38.00 | 42.56 |
| P06 | Shampoo | 245.00 | 274.40 |
(g) Display the total number of products manufactured by each manufacturer.
Use GROUP BY with COUNT(*):
SELECT Manufacturer, COUNT(*) AS TotalProducts
FROM Product
GROUP BY Manufacturer;
Expected output (based on original data before updates):
| Manufacturer | TotalProducts |
|---|---|
| Colgate | 1 |
| Dove | 2 |
| Lux | 1 |
| Pepsodent | 1 |
| Surf | 1 |
(h) SELECT PName, avg(UPrice) FROM Product GROUP BY Pname;
This groups by product name and computes the average price for each group. Note: the original data has two "Soap" rows (25 and 38) and two "Toothpaste" rows (54 and 65).
Expected output:
| PName | avg(UPrice) |
|---|---|
| Washing Powder | 120.000000 |
| Toothpaste | 59.500000 |
| Soap | 31.500000 |
| Shampoo | 245.000000 |
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.