Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NOTE: Written in SQL Express. Using \"MyGuitarShop database\". Orders Table: PK.

ID: 3784887 • Letter: N

Question

NOTE: Written in SQL Express. Using "MyGuitarShop database".

Orders Table:

PK. OrderId, FK CustomerID

OrderItems Table:

PK. ItemID, FK OrderID, FK ProductID, Columns: ItemPrice, DiscountAmount, Quantity.

1. Write a script that uses three variables to store (1) the count of distinct orders made in the Orders table (2) the total amount of sales (sales means the price of the item with discount multiplied by the quantity) made in the OrderItems table and (3) the average sales calculated using the first two variables ($843.3). Don’t include TaxAmount or shipping costs.

Explanation / Answer

DECLARE
order_count INT;
sales_price NUMBER(20,2);
BEGIN

SELECT COUNT(distinct OrderID) INTO order_count
FROM Orders;

SELECT SUM((ItemPrice - DiscountAmount) * DiscountAmount) INTO sales_price
FROM OrderItems;

END;