0% found this document useful (0 votes)
12 views5 pages

SQL Assignment

Uploaded by

Rashid Malik
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)
12 views5 pages

SQL Assignment

Uploaded by

Rashid Malik
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/ 5

/*

Creates schema and data for Questions

Below the DDL and DML are additional tasks that should be
performed, in a single separate file.
please provide the code for all tasks including the answers
for the 4 free form text questions

*/
----------------------------------------------------------------------
---------
---- Start - Schema and Data
----------------------------------------------------------------------
---------

DROP TABLE IF EXISTS dbo.FoodTypes;

CREATE TABLE dbo.FoodTypes (FoodTypeID INT IDENTITY(1, 1) PRIMARY KEY,


FoodType VARCHAR(50) NOT NULL);

INSERT INTO dbo.FoodTypes (FoodType) VALUES ('Fruits'), ('Meats'),


('Drinks'), ('Other');

DROP TABLE IF EXISTS dbo.Foods;

CREATE TABLE dbo.Foods


(
FoodID INT IDENTITY(1, 1) PRIMARY KEY,
FoodTypeID INT NOT NULL,
FoodName VARCHAR(50) NOT NULL,
Price NUMERIC(5, 2) NOT NULL
);
GO

DROP PROC IF EXISTS dbo.AddFood;


GO

CREATE PROC dbo.AddFood (@FoodName VARCHAR(50), @FoodType VARCHAR(50),


@Price NUMERIC(5, 2))
AS
BEGIN
INSERT INTO dbo.Foods (FoodName, FoodTypeID, Price)
SELECT @FoodName, FoodTypeID, @Price FROM dbo.FoodTypes WHERE
FoodType = @FoodType;
END;
GO

--sql prompt formatting off


EXEC dbo.AddFood @FoodName = 'Apples', @FoodType = 'Fruits', @Price =
4.50;
EXEC dbo.AddFood @FoodName = 'Orange', @FoodType = 'Fruits', @Price =
2.99;
EXEC dbo.AddFood @FoodName = 'Pineapple', @FoodType = 'Fruits', @Price
= 6.25;
EXEC dbo.AddFood @FoodName = 'Walnuts', @FoodType = 'Other', @Price =
16.25;
EXEC dbo.AddFood @FoodName = 'Bread', @FoodType = 'Other', @Price =
3.50;
EXEC dbo.AddFood @FoodName = 'Strawberries', @FoodType = 'Fruits',
@Price = 8.50;
EXEC dbo.AddFood @FoodName = 'Milk', @FoodType = 'Drinks', @Price = 5;
EXEC dbo.AddFood @FoodName = 'Water', @FoodType = 'Drinks', @Price =
0.99;
EXEC dbo.AddFood @FoodName = 'Chicken', @FoodType = 'Meats', @Price =
5.99;
EXEC dbo.AddFood @FoodName = 'Ny Strip', @FoodType = 'Meats', @Price =
12.99;
EXEC dbo.AddFood @FoodName = 'Pork Ribs', @FoodType = 'Meats', @Price
= 6.99;
EXEC dbo.AddFood @FoodName = 'Chorizo', @FoodType = 'Meats', @Price =
3.99;
--sql prompt formatting on
DROP TABLE IF EXISTS dbo.ShoppingList;

CREATE TABLE dbo.ShoppingList (ShoppingListID INT IDENTITY(1, 1)


PRIMARY KEY, FoodID INT NOT NULL, Quantity INT NOT NULL);
GO

DROP PROC IF EXISTS dbo.AddToShoppingList;


GO

CREATE PROC dbo.AddToShoppingList (@FoodName VARCHAR(50), @Quantity


INT)
AS
BEGIN
INSERT INTO dbo.ShoppingList (FoodID, Quantity) SELECT FoodID,
@Quantity FROM dbo.Foods WHERE FoodName = @FoodName;
END;
GO

--sql prompt formatting off


EXEC dbo.AddToShoppingList @FoodName = 'Apples', @Quantity = 5;
EXEC dbo.AddToShoppingList @FoodName = 'Apples', @Quantity = 6;
EXEC dbo.AddToShoppingList @FoodName = 'Bread', @Quantity = 2;
EXEC dbo.AddToShoppingList @FoodName = 'Milk', @Quantity = 1;
EXEC dbo.AddToShoppingList @FoodName = 'Water', @Quantity = 8;
EXEC dbo.AddToShoppingList @FoodName = 'Orange', @Quantity = 6;
EXEC dbo.AddToShoppingList @FoodName = 'Pineapple', @Quantity = 1;
EXEC dbo.AddToShoppingList @FoodName = 'Pineapple', @Quantity = 1;
EXEC dbo.AddToShoppingList @FoodName = 'Chicken', @Quantity = 2;
EXEC dbo.AddToShoppingList @FoodName = 'Chicken', @Quantity = 5;
EXEC dbo.AddToShoppingList @FoodName = 'Chicken', @Quantity = 3;
EXEC dbo.AddToShoppingList @FoodName = 'Ny Strip', @Quantity = 4;
EXEC dbo.AddToShoppingList @FoodName = 'Pork Ribs', @Quantity = 1;
EXEC dbo.AddToShoppingList @FoodName = 'Chorizo', @Quantity = 2;
--sql prompt formatting on
GO
----------------------------------------------------------------------
---------
---- END - Schema and Data
----------------------------------------------------------------------
---------
GO
----------------------------------------------------------------------
---------
---- TASKS
----------------------------------------------------------------------
---------

----------------------------------------------------------------------
---------
-- Task 1
-- Create a view named "dbo.vShoppingList" that will show FoodName,
Quantity, Price, TotalAmount

----------------------------------------------------------------------
---------
-- Task 2
-- Write a view called "vShoppingListTotals" that shows that
TotalAmount spent on each food type
-- Include a column that shows the Food Type Total Amount as a
percentage of the overall shopping cart total
---Solution should be a single query, with no corelated queries, and
no CTE's

----------------------------------------------------------------------
---------
-- Task 3.
-- Write a query to only show the duplicated food in the ShoppingList

----------------------------------------------------------------------
---------
-- Task 4.
-- Write a single query delete statement that will remove the
duplicates, leaving the one with more quantity
--Delete should be a single statement without hardcoding any ids

----------------------------------------------------------------------
---------
-- Task 5.
---- Create a scalar function called "fnShoppingItemWithHighestPrice"
to which you will pass @Price parameter, and it will return the most
expensive FoodId under that price in your shopping cart, individual
price for a food
----------------------------------------------------------------------
---------
-- Task 6.
--Execute the function two times
---1. What food item is returned when the input is 21.11?
---2. What food item is returned when the input is 6.00?

----------------------------------------------------------------------
---------
-- Task 7.
--Create view "vShoppingListRunningTotal"
-- that will list all foods,and show a "RunningTotal" of their prices.
Ordered by Food Name

----------------------------------------------------------------------
---------
-- Task 8.
-- Create a stored procedure called "uspUpsertShoppingList" to handle
both inserts and updates of the shopping list (e.g. - if the item
exists, update its quantity, if it doesn't exist, insert it)
--- execute this new procedure for Apples, updating the quantity to 10
--- execute this new procedure for Walnuts, with a quantity of 1

----------------------------------------------------------------------
---------
-- Task 9.
-- Create a view "vShoppingCartFoodTypePivot" that will Pivot on known
FoodTypes while Summing the TotalCost (Price*Quantity) for each food
type
-- A sample format would look like
-- Drinks -- Fruits
-- 11.11 -- 2.22
--should be a single query with no case statements

----------------------------------------------------------------------
---------
-- Task 10.
-- CREATE foreign keys for the tables

----------------------------------------------------------------------
---------
-- Task 11.
-- Create code to prevent duplicates in the Foods,FoodTypes, and
ShoppingList
-- Create code to prevent the ShoppingList quantity from exceeding 99

----------------------------------------------------------------------
---------
-- Task 12
-- Devise a single stored procedure "uspTest" that will perform the
following tasks
-- uspUpsertShoppingList changing water to 25
-- AddToShoppingList to add pork ribs with a quantity of 1
-- Within this procedure we need a TRY CATCH and DML actions need to
be contained within a transaction.
-- In the event of an exception, we need to catch it, undo changes,
and return a user safe message

----------------------------------------------------------------------
---------
---- TASKS
----------------------------------------------------------------------
---------
-- Question 1 - What are the Pros or Cons of a corelated query as
such?
/*
SELECT
*,
(SELECT Fd.FoodType FROM dbo.FoodTypes AS Fd WHERE
Fd.FoodTypeID = Foods.FoodTypeID) AS FoodTypes
FROM dbo.Foods AS Foods;
*/
-- Question 2. If we were to extend this shopping carts to be utilized
for multiple persons, what steps and changes would need to occur?
-- Question 3. If we were interested in tracking all changes made to
the shopping cart, what could we use?
-- Question 4. If you were requested to fix the following query, how
would you approach it?
/*
SELECT *, dbo.fnShoppingItemWithHighestPrice(Price) AS
NextHighestPrice
FROM dbo.Foods
WHERE Price < dbo.fnShoppingItemWithHighestPrice(100);
*/

You might also like