SQL Assignment
SQL Assignment
Syndicate- 2
PRN Name
Solution Query:
USE TShirts; -- Switch to the TShirts database
CREATE TABLE Customer_Master (
CustomerID NVARCHAR(50) PRIMARY KEY,
RegistrationDate DATE,
Mobile_Number NVARCHAR(20),
Customer_Name NVARCHAR(100) NOT NULL,
EMailId NVARCHAR(100),
Age INT CHECK (Age BETWEEN 18 AND 60),
City NVARCHAR(100),
Pincode INT CHECK (Pincode >= 0 AND Pincode <= 999999)
);
b. Table Pune_Sales having following specifications.
Solution Query:
USE TShirts; -- Assuming you're still working in the TShirts database
CREATE TABLE Pune_Sales (
OrderID INT PRIMARY KEY,
OrderDate DATE,
CustomerID NVARCHAR(50),
FOREIGN KEY (CustomerID) REFERENCES Customer_Master(CustomerID),
Sleeve_Length NVARCHAR(100),
Color NVARCHAR(100),
Pattern NVARCHAR(100),
Neck_Style NVARCHAR(100),
Size NVARCHAR(100),
Price_Per_TShirt NUMERIC(10, 2) CHECK (Price_Per_TShirt <= 3000),
Quantity INT
);
c. Table Mumbai_Sales having following specifications.
Soultion Query:
USE TShirts; -- Assuming you're still working in the TShirts database
CREATE TABLE Mumbai_Sales (
OrderID INT PRIMARY KEY,
OrderDate DATE,
CustomerID NVARCHAR(50),
FOREIGN KEY (CustomerID) REFERENCES Customer_Master(CustomerID),
Sleeve_Length NVARCHAR(100),
Color NVARCHAR(100),
Pattern NVARCHAR(100),
Neck_Style NVARCHAR(100),
Size NVARCHAR(100),
Price_Per_TShirt NUMERIC(10, 2) CHECK (Price_Per_TShirt <= 3000),
Quantity INT
);
3. About Records in the table:
Solution Query:
-- Inserting sample customer records into Customer_Master table
INSERT INTO Customer_Master (CustomerID, RegistrationDate, Mobile_Number,
Customer_Name, EMailId, Age, City, Pincode)
VALUES
('CUST001', '2023-01-01', '1234567890', 'John Doe', 'john.doe@example.com', 25, 'New
York', 10001),
('CUST002', '2023-01-02', '9876543210', 'Jane Smith', 'jane.smith@example.com', 30, 'Los
Angeles', 90001),
('CUST003', '2023-01-03', '5551234567', 'Alice Johnson', 'alice.johnson@example.com', 35,
'Chicago', 60601),
('CUST004', '2023-01-04', '1112223333', 'Bob Williams', 'bob.williams@example.com', 40,
'Houston', 77001),
('CUST005', '2023-01-05', '9998887777', 'Mary Brown', 'mary.brown@example.com', 45,
'Philadelphia', 19101),
('CUST006', '2023-01-06', '7776665555', 'David Jones', 'david.jones@example.com', 50,
'Phoenix', 85001),
('CUST007', '2023-01-07', '3334445555', 'Sarah Davis', 'sarah.davis@example.com', 55, 'San
Antonio', 78201),
('CUST008', '2023-01-08', '6667778888', 'Michael Wilson', 'michael.wilson@example.com',
60, 'San Diego', 92101),
('CUST009', '2023-01-09', '2223334444', 'Emily Martinez', 'emily.martinez@example.com',
25, 'Dallas', 75201),
('CUST010', '2023-01-10', '4445556666', 'James Taylor', 'james.taylor@example.com', 30,
'San Jose', 95101);
4. Views should be created which will depict the overall sales reports.
Following SQL techniques should be used while creating views:
a. Filtering records using logical And, Or operators.
b. Joins
c. Expression Columns and Built-In functions
d. Set operators.
e. Aggregate Functions & Summary Queries
Minimum 5 views should be created which will have the above functionality.
Solution :
1. View 1: Total Sales
- This view provides the total sales from both Pune and Mumbai.
- It uses aggregate functions for summarizing the total quantity and sales amount.
- It also employs joins to link sales data with customer information.
Query:
CREATE VIEW Total_Sales AS
SELECT
SUM(ps.Quantity) + SUM(ms.Quantity) AS Total_Quantity,
SUM(ps.Quantity * ps.Price_Per_TShirt) + SUM(ms.Quantity * ms.Price_Per_TShirt) AS
Total_Sales_Amount
FROM
Pune_Sales ps
JOIN
Customer_Master cm ON ps.CustomerID = cm.CustomerID
JOIN
Mumbai_Sales ms ON ps.CustomerID = ms.CustomerID;
2. View 2: Sales by Color
- This view presents the sales breakdown by color across both Pune and Mumbai.
- It utilizes aggregate functions to calculate total sales for each color.
Query:
CREATE VIEW Sales_By_Color AS
SELECT
Color,
SUM(Quantity) AS Total_Quantity,
SUM(Quantity * Price_Per_TShirt) AS Total_Sales_Amount
FROM
(
SELECT Color, Quantity, Price_Per_TShirt FROM Pune_Sales
UNION ALL
SELECT Color, Quantity, Price_Per_TShirt FROM Mumbai_Sales
) AS All_Sales
GROUP BY
Color;
3. View 3: Sales by Size and Sleeve Length
- This view shows sales data categorized by size and sleeve length.
- It employs expression columns to concatenate size and sleeve length for a more
descriptive view.
Query:
CREATE VIEW Sales_By_Size_Sleeve AS
SELECT
CONCAT(ps.Size, ' - ', ps.Sleeve_Length) AS Size_Sleeve,
SUM(ps.Quantity) AS Total_Quantity,
SUM(ps.Quantity * ps.Price_Per_TShirt) AS Total_Sales_Amount
FROM
Pune_Sales ps
GROUP BY
Size_Sleeve;
4. View 4: Customers with High Purchases**
- This view lists customers who have made high-value purchases.
- It uses aggregate functions to identify customers with the highest total spending.
Query:
CREATE VIEW High_Purchase_Customers AS
SELECT
cm.CustomerID,
cm.Customer_Name,
SUM(ps.Quantity * ps.Price_Per_TShirt) + SUM(ms.Quantity * ms.Price_Per_TShirt) AS
Total_Purchase_Amount
FROM
Customer_Master cm
LEFT JOIN
Pune_Sales ps ON cm.CustomerID = ps.CustomerID
LEFT JOIN
Mumbai_Sales ms ON cm.CustomerID = ms.CustomerID
GROUP BY
cm.CustomerID,
cm.Customer_Name
HAVING
Total_Purchase_Amount > 5000; -- Adjust this threshold as needed
Query:
CREATE VIEW Combined_Sales_Report AS
SELECT 'Total Sales' AS Category, Total_Quantity, Total_Sales_Amount FROM Total_Sales
UNION ALL
SELECT 'Sales by Color' AS Category, 0 AS Total_Quantity, 0 AS Total_Sales_Amount
FROM Sales_By_Color
UNION ALL
SELECT 'Sales by Size and Sleeve Length' AS Category, 0 AS Total_Quantity, 0 AS
Total_Sales_Amount FROM Sales_By_Size_Sleeve;