Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

SQL Assignment

Uploaded by

mansha bhatia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL Assignment

Uploaded by

mansha bhatia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

IBI Component-3

SQL LAB ASSIGNMENT

Syndicate- 2

PRN Name

23020542008 Vishruth Bhat

23020542009 Mansha Bhatia

23020542010 Rishab Bhutoria

23020542011 Adrija Bose

23020542012 Srishti Chakraborty

23020542013 Raushan Chaudhary


SQL Lab Assignment

1. In SQL Server create a database with the name as TShirts

Solution Query: CREATE DATABASE TShirts;

2. In the Database TShirts following relational tables must be created.


a. Table Customer_Master having following specifications.

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:

a. Minimum 10 customer records must be added in the Customer_Master table.


b. Minimum 30 records must be added in both Pune_Sales and Mumbai_Sales
tables.
c. Sleeve Length values can be Full Sleeve or Half Sleeve or Sleeveless.
d. Color values can be Red or Blue or Green or White or Black or Yellow.
e. Pattern values can be Striped or Solid.
f. Neck Style values can be Round or Collar or V Shape.
g. Size values can be S or M or L or XL or XXL.

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

-- Inserting sample records into Pune_Sales table


DECLARE @counter INT = 1;
WHILE @counter <= 30
BEGIN
INSERT INTO Pune_Sales (OrderID, OrderDate, CustomerID, Sleeve_Length, Color,
Pattern, Neck_Style, Size, Price_Per_TShirt, Quantity)
VALUES
(@counter, DATEADD(day, -@counter, GETDATE()), 'CUST00' + CAST((@counter %
10) + 1 AS VARCHAR),
CASE
WHEN @counter % 3 = 0 THEN 'Full Sleeve'
WHEN @counter % 3 = 1 THEN 'Half Sleeve'
ELSE 'Sleeveless'
END,
CASE
WHEN @counter % 6 = 0 THEN 'Red'
WHEN @counter % 6 = 1 THEN 'Blue'
WHEN @counter % 6 = 2 THEN 'Green'
WHEN @counter % 6 = 3 THEN 'White'
WHEN @counter % 6 = 4 THEN 'Black'
ELSE 'Yellow'
END,
CASE
WHEN @counter % 2 = 0 THEN 'Striped'
ELSE 'Solid'
END,
CASE
WHEN @counter % 3 = 0 THEN 'Round'
WHEN @counter % 3 = 1 THEN 'Collar'
ELSE 'V Shape'
END,
CASE
WHEN @counter % 5 = 0 THEN 'S'
WHEN @counter % 5 = 1 THEN 'M'
WHEN @counter % 5 = 2 THEN 'L'
WHEN @counter % 5 = 3 THEN 'XL'
ELSE 'XXL'
END,
1500.00,
2);
SET @counter = @counter + 1;
END;

-- Inserting sample records into Mumbai_Sales table


SET @counter = 1;
WHILE @counter <= 30
BEGIN
INSERT INTO Mumbai_Sales (OrderID, OrderDate, CustomerID, Sleeve_Length, Color,
Pattern, Neck_Style, Size, Price_Per_TShirt, Quantity)
VALUES
(@counter, DATEADD(day, -@counter, GETDATE()), 'CUST00' + CAST((@counter %
10) + 1 AS VARCHAR),
CASE
WHEN @counter % 3 = 0 THEN 'Full Sleeve'
WHEN @counter % 3 = 1 THEN 'Half Sleeve'
ELSE 'Sleeveless'
END,
CASE
WHEN @counter % 6 = 0 THEN 'Red'
WHEN @counter % 6 = 1 THEN 'Blue'
WHEN @counter % 6 = 2 THEN 'Green'
WHEN @counter % 6 = 3 THEN 'White'
WHEN @counter % 6 = 4 THEN 'Black'
ELSE 'Yellow'
END,
CASE
WHEN @counter % 2 = 0 THEN 'Striped'
ELSE 'Solid'
END,
CASE
WHEN @counter % 3 = 0 THEN 'Round'
WHEN @counter % 3 = 1 THEN 'Collar'
ELSE 'V Shape'
END,
CASE
WHEN @counter % 5 = 0 THEN 'S'
WHEN @counter % 5 = 1 THEN 'M'
WHEN @counter % 5 = 2 THEN 'L'
WHEN @counter % 5 = 3 THEN 'XL'
ELSE 'XXL'
END,
2000.00,
3);
SET @counter = @counter + 1;
END;

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

5. View 5: Combined Sales Report


- This view provides a comprehensive summary of sales data, including total sales, sales by
color, and sales by size and sleeve length.
- It utilizes set operators to combine data from multiple queries.

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;

You might also like