0% found this document useful (0 votes)
7 views

Quuzzzzzz

Uploaded by

Syi an
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Quuzzzzzz

Uploaded by

Syi an
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

-- Initial Setup: Create database, use it, and create two tables (Customer, Product)

CREATE DATABASE Quiz4_Data; -- Creates the database


USE Quiz4_Data; -- Switches to the Quiz4_Data database

-- Create Customer table with primary key


CREATE TABLE Customer (
Customer_ID CHAR(3) NOT NULL UNIQUE, -- Customer ID (Primary Key)
FName VARCHAR(20) NOT NULL, -- Customer's first name
LName VARCHAR(20) NOT NULL, -- Customer's last name
Gender CHAR(1) NOT NULL, -- Customer's gender (M/F)
CONSTRAINT PRIMARY KEY (Customer_ID) -- Primary key constraint
);

-- Create Product table with primary key


CREATE TABLE Product (
Product_ID CHAR(4) NOT NULL UNIQUE, -- Product ID (Primary Key)
Brand VARCHAR(20) NOT NULL, -- Product brand
PName VARCHAR(50) NOT NULL, -- Product name
Expiry_date DATE, -- Product expiry date
Price INT2, -- Product price (2-byte integer)
CONSTRAINT PRIMARY KEY (Product_ID) -- Primary key constraint
);

-- Insert records into Customer table


INSERT INTO Customer VALUES
("c01", "Mario", "Maurer", "M"),
("c02", "Nadech", "Kugimiya", "M"),
("c03", "Ranee", "Campen", "F"),
("c04", "Darika", "Hoorne", "F");

-- Insert records into Product table


INSERT INTO Product VALUES
("M001", "Meiji Milk", "Meiji High Protein Milk", "2023-12-25", 15),
("M002", "Meiji Milk", "Meiji Yoghurt Milk", "2023-11-28", 30),
("B001", "Farmhouse", "Whole Wheat Bread", "2023-11-21", 35),
("B002", "Farmhouse", "Mini Bun Bread", "2023-11-23", 28);

-- 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
);

-- Insert records into Payment table


INSERT INTO Payment VALUES
('c01', 'M001', '2023-11-01', 22),
('c02', 'B001', '2023-09-12', 30),
('c02', 'M002', '2023-09-12', 12),
('c01', 'B002', '2023-11-01', 5),
('c02', 'M001', '2023-11-15', 10),
('c03', 'M002', '2023-11-16', 11);

-- 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

-- Q3: Calculate total amount paid for each month in 2023


SELECT
MonthName,
TotalAmount
FROM (
SELECT
MONTH(Paid_Date) AS MonthNumber, -- Extracts month number
MONTHNAME(Paid_Date) AS MonthName, -- Extracts month name
SUM(Amount) AS TotalAmount -- Sums the payment amounts
FROM Payment
WHERE YEAR(Paid_Date) = 2023 -- Filters for the year 2023
GROUP BY MonthNumber, MonthName -- Groups by month number and name
) AS MonthlyData
ORDER BY MonthlyData.MonthNumber; -- Orders results by month number

-- 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

You might also like