0% found this document useful (0 votes)
21 views7 pages

Ahtisham Ali Lab6

The document describes a lab assignment to create a database called lab6 with a Product table. It inserts sample data and defines several stored procedures to retrieve and manipulate data from the Product table, including finding average price, products below average, dropping tables, retrieving by price range, counting total products, finding by name, and counting by category.

Uploaded by

Ahtisham Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

Ahtisham Ali Lab6

The document describes a lab assignment to create a database called lab6 with a Product table. It inserts sample data and defines several stored procedures to retrieve and manipulate data from the Product table, including finding average price, products below average, dropping tables, retrieving by price range, counting total products, finding by name, and counting by category.

Uploaded by

Ahtisham Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DBAM Lab

Ahtisham Ali
F2020105001

Lab # 6
create database lab6;

use lab6;

CREATE TABLE Product (

ProductID INT PRIMARY KEY,

ProductName VARCHAR(255),

Price DECIMAL(10, 2),

ProductCategory VARCHAR(50),

ProductCompany VARCHAR(50),

ProductManufacturer VARCHAR(50),

ProductSupplier VARCHAR(50)

);

INSERT INTO Product (ProductID, ProductName, Price, ProductCategory, ProductCompany,


ProductManufacturer, ProductSupplier)

VALUES

(1, 'ultrabook', 1099.99, 'Electronics', 'new electronics Inc.', 'ManufacturerA', 'SupplierX'),

(2, 'tablet', 349.99, 'Electronics', 'XYZ tech', 'ManufacturerB', 'SupplierNew'),

(3, 'office chair', 199.99, 'Furniture', 'Furniture CO', 'ManufacturerC', 'SupplierZ'),

(4, 'blender', 39.99, 'Appliances', 'home appliances Inc.', 'ManufacturerD', 'SupplierW'),

(5, 'sneakers', 89.99, 'apparel', 'SportGear ltd.', 'ManufacturerE', 'SupplierV');

DELIMITER //

CREATE PROCEDURE GetAveragePrice()

BEGIN

DECLARE avg_price DECIMAL(10, 2);

SELECT AVG(Price) INTO avg_price

FROM Product;
SELECT avg_price AS AveragePrice;

END //

DELIMITER ;

CALL GetAveragePrice();

CREATE PROCEDURE GetLowestPriceProducts()

BEGIN

SELECT * FROM Product

WHERE Price < (SELECT AVG(Price) FROM Product);

END //

DELIMITER ;

CALL GetLowestPriceProducts();
DELIMITER //

CREATE PROCEDURE DropAllObjects()

BEGIN

DROP TABLE IF EXISTS Product;

END //

DELIMITER ;

CALL DropAllObjects();

DELIMITER //

CREATE PROCEDURE GetProductsByPriceRange(IN minPrice DECIMAL(10, 2), IN maxPrice DECIMAL(10,


2))

BEGIN

SELECT * FROM Product

WHERE Price BETWEEN minPrice AND maxPrice;

END //

DELIMITER ;

CALL GetProductsByPriceRange(50.00, 100.00);


DELIMITER //

CREATE PROCEDURE GetTotalProducts()

BEGIN

SELECT COUNT(*) AS TotalProducts FROM Product;

END //

DELIMITER ;

CALL GetTotalProducts();

DELIMITER //

CREATE PROCEDURE GetAverageProductPrice()

BEGIN

SELECT AVG(Price) AS AveragePrice FROM Product;

END //
DELIMITER ;

CALL GetAverageProductPrice();

DELIMITER //

CREATE PROCEDURE FindProductByName(IN productName VARCHAR(255))

BEGIN

SELECT *

FROM Product

WHERE ProductName = productName;

END //

DELIMITER ;

CALL FindProductByName('YourProductName');
DELIMITER //

CREATE PROCEDURE GetProductsCountByCategory(IN productCategory VARCHAR(50))


BEGIN
SELECT COUNT(*) AS TotalProducts
FROM Product
WHERE ProductCategory = productCategory;
END //

DELIMITER ;
CALL GetProductsCountByCategory('YourProductCategory');

You might also like