Ujjwal Singh
0801IT211090
LabAssignment-IV
TableName:Manufacturer
Attribute Data Type Primary Foreign Constraint
Code Int Y NotNull
Name Char NotNull
CREATE DATABASE UJJWAL_assignment4;
USE UJJWAL_assignment4;
CREATE TABLE Manufacturer(Code int PRIMARY KEY NOT NULL ,Name char NOT NULL);
TableName:Products
Attribute Data Type Primary Foreign Constraint
Code INT Y NotNull
Name Char NotNull
Price Decimal NotNull
Manufacturer Int Manufacturer(Code NotNull
)
CREATE TABLE Products(Code int PRIMARY KEY NOT NULL, Namechar NOT NULL,
Price decimal NOT NULL,Manufacturer int NOT NULL,
FOREIGN KEY(Manufacturer) REFERENCES Manufacturer(Code) On DELETE SET NULL
);
Ujjwal Singh
0801IT211090
Insert following data into Manufacturer Table.
Code Name
1 Sony
2 CreativeLabs
3 Hewlett-Packard
4 Iomega
5 Fujitsu
6 Winchester
INSERT INTO Manufacturer(Code,Name)
VALUES('1','Sony'),('2','Creative Labs'),('3','Hewlett-Packard'),('4','Iomega'),('5 ','Fujitsu'),
('6','Winchester');
Insertfollowing datainto Products Table.
Code Name Price Manufacturer
1 HardDrive 240 5
2 Memory 120 6
3 ZipDrive 150 4
4 FloppyDisk 5 6
5 Monitor 240 1
6 DVDDrive 180 2
7 CD Drive 90 2
Ujjwal Singh
0801IT211090
8 Printer 270 3
9 ToneCartridge 66 3
10 DVDBurner 180 2
INSERTINTOProducts(Code,Name,Price,Manufacturer)
VALUES(1,'HardDrive',240,5),(2,'Memory',120,6),(3,'ZipDrive',150,4),(4,'FloppyDisk',5,6),
(5, 'Monitor', 240 ,1),(6 ,'DVD Drive' ,180 ,2),(7 ,'CD Drive' ,90 ,2),(8 ,'Printer',270 ,3), (9,'Tone
Cartridge',66 ,3), (10 ,'DVD Burner',180 ,2);
1. Selectthenamesofallproductinthestore.
CODE: SELECT Name FROM Products;
2. Selectthenamesandthepricesofalltheproductsinthestore.
CODE: SELECT Name,Price FROM Products;
Ujjwal Singh
0801IT211090
3. Select the name of products with price less than or equal to $200.
CODE:SELECTNameFROMProductsWHEREPrice<200ORPrice=200;
4. Select all the products with a price between $60 and $120.
CODE:SELECT Name FROM Products WHERE Price BETWEEN 60 and 120;
5. Selectnameandpriceincents(i.e.thepricemultiplyby100).
CODE: SELECT Name,Price*100 AS Cents FROM Products;
Ujjwal Singh
0801IT211090
6. Compute the average price of all the product.
CODE:SELECTAVG(Price)ASAverageFROMProducts;
7. Compute average price of all products with manufacturer code equal to 2.
CODE:SELECTAVG(Price)ASAverageFROMProductsWHEREManufacturer=2;
8. Computethenumberofproductswithapricelargerthanorequalto$180.
CODE: SELECT COUNT(Name) FROM Products WHERE Price>=180;
9. Selectthenameandpriceofallproductswithapricelargerthanorequalto$180and,sort first
byprice (in descending order), and then by name(in ascending order).
CODE:SELECTName,PriceFROMProductsWHEREPrice>=180 ORDER
BY Price DESC,Name ASC;
Ujjwal Singh
0801IT211090
10. Selectallthedatafromproducts,includingallthedataforeachproducts
manufacturer.
CODE:SELECTProducts.*,Manufacturer.*FROM Products
JOINManufacturer ON Products.Manufacturer=Manufacturer.Code;
11. Select the product name, Price , and manufacturer names of all products.
CODE:SELECTProducts.NameASProductName,Products.Price,Manufacturer.NameAS
ManufacturerName FROM Products
JOINManufacturer ON Products.Manufacturer=Manufacturer.Code;
Ujjwal Singh
0801IT211090
12. Selecttheaveragepriceofeachmanufacturersproducts,showingonlythemanufacturercode.
CODE:SELECT Manufacturer,AVG(PRice) as Average FROM Products
GROUP BY Manufacturer;
13. Selecttheaveragepriceofeachmanufacturersproducts,Showingthemanufacturers
name.
CODE:SELECTman.NameasManufacturerName,AVG(p.Price)asAverageFROMProducts p
JOIN Manufacturer man ON p.Manufacturer=man.Code GROUP BY man.Name;
14. Selectthenameofmanufacturerswhoseproducthaveanaveragepricelargerthanor equal
to $150.
CODE:SELECTman.NameasManufacturer_NameFROMManufacturerman JOIN
Products p ON man.Code=p.Manufacturer GROUP BY man.Name HAVING
AVG(p.Price)>=150;
Ujjwal Singh
0801IT211090
15. Select the name and price of the cheapest product.
CODE: SELECT Name,Price FROM Products ORDER BY Price ASC LIMIT 1;
16. Selectthenameofeachmanufactureralongwiththenameandpriceofitsmostexpensive product.
CODE:SELECTman.NameasManufacturer_Name,p.NameASProducts_Name,
p.Price AS Products_Price FROM Manufacturer man
JOINProductspONman.Code=p.ManufacturerWHERE(p.Price,p.Manufacturer)IN (SELECT
MAX(Price), Manufacturer FRom Products GROUP by Manufacturer);
17. Add a new product: Loudspeakers, $70, Manufacturer 2.
CODE:INSERTINTOProducts(Code,Name,Price,Manufacturer)
VALUES(11,'Loudspeakers',70,2);
Ujjwal Singh
0801IT211090
18. Update the name of product 8 to “Laser printer”.
CODE:UPDATEProductsSETName='Laserprinter'WHERECode=8;
19. Apply a 10% discount to all products.
CODE: SELECT Name,PRICE*0.9 AS Discount FROM Products;
20. Apply a 10% discount to all products with a price larger than or equal to $120.
CODE:SELECTName,PRICE*0.9ASDiscountFROMProductsWHEREPrice>=120;