Quuzzzzzz
Quuzzzzzz
-- Create Payment table with foreign key references to Customer and Product
CREATE TABLE IF NOT EXISTS Payment (
Customer_ID CHAR(3) NOT NULL, -- Customer ID (Foreign Key)
Product_ID CHAR(4) NOT NULL, -- Product ID (Foreign Key)
Paid_Date DATE NOT NULL, -- Payment date
Amount INT NOT NULL, -- Payment amount
CONSTRAINT pk_Payment PRIMARY KEY (Customer_ID, Product_ID), -- Primary key
CONSTRAINT fk_Payment_Customer FOREIGN KEY (Customer_ID) REFERENCES Customer(Customer_ID), -- Foreign key to Customer
CONSTRAINT fk_Payment_Product FOREIGN KEY (Product_ID) REFERENCES Product(Product_ID) -- Foreign key to Product
);
-- Q2: Capitalize the first letter of the brand and filter product names containing 'te' or 'le'
SELECT
CONCAT(UCASE(LEFT(Brand, 1)), SUBSTRING(Brand, 2)) AS ProductBrand, -- Capitalizes the first letter
PName AS ProductName
FROM Product
WHERE PName LIKE '%te%' OR PName LIKE '%le%' -- Filters product names with 'te' or 'le'
ORDER BY PName; -- Orders results by product name
-- Q4: Find max price for products expiring in November 2023, grouped by brand
SELECT
Brand AS `Product Brand`,
CAST(MAX(Price) AS DECIMAL(10, 2)) AS `Max Price` -- Finds max price, casts to decimal
FROM Product
WHERE MONTHNAME(Expiry_Date) = 'November' AND YEAR(Expiry_Date) = 2023 -- Filters by November 2023 expiry date
GROUP BY Brand -- Groups by brand
HAVING `Max Price` > 20.00 -- Filters for products with max price greater than 20
ORDER BY `Max Price`; -- Orders by price in ascending order
-- Q5: Retrieve purchase details for 'Mario Maurer' between November 1, 2023, and December 31, 2023
SELECT
c.fullName AS `Full Name`, -- Customer full name
prod.PName AS `Product Name`, -- Product name
p.Amount AS `Total Amount` -- Payment amount
FROM Payment AS p
JOIN (
SELECT
Customer_ID,
CONCAT(FName, ' ', LName) AS fullName -- Concatenates first and last name for 'Mario Maurer'
FROM Customer
WHERE CONCAT(FName, ' ', LName) = 'Mario Maurer' -- Filters for Mario Maurer
) AS c
ON p.Customer_ID = c.Customer_ID
JOIN Product AS prod
ON p.Product_ID = prod.Product_ID
WHERE Paid_Date BETWEEN '2023-11-01' AND '2023-12-31' -- Filters payment date range
ORDER BY Amount DESC; -- Orders by total amount in descending order