Skip to content
Exercises · Q7
Q.

Consider the following table named "Product", showing details of products being sold in a grocery shop.

PCodePNameUPriceManufacturer
P01Washing Powder120Surf
P02Toothpaste54Colgate
P03Soap25Lux
P04Toothpaste65Pepsodent
P05Soap38Dove
P06Shampoo245Dove

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;

West Bengal WbchseTextbookSubjective· 4mImportance★★★★★
49% · 47/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 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
);
Tip

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.

Watch out

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:

PCodePNameUPrice
P06Shampoo245.00
P03Soap25.00
P05Soap38.00
P02Toothpaste54.00
P04Toothpaste65.00
P01Washing Powder120.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:

PCodePNameUPriceManufacturerDiscount
P01Washing Powder120.00Surf12.00
P02Toothpaste54.00Colgate0.00
P03Soap25.00Lux0.00
P04Toothpaste65.00Pepsodent0.00
P05Soap38.00Dove0.00
P06Shampoo245.00Dove24.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:

PCodePNameUPrice (before)UPrice (after)
P05Soap38.0042.56
P06Shampoo245.00274.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):

ManufacturerTotalProducts
Colgate1
Dove2
Lux1
Pepsodent1
Surf1

(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:

PNameavg(UPrice)
Washing Powder120.000000
Toothpaste59.500000
Soap31.500000
Shampoo245.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.