-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_Sales_Project.sql
More file actions
259 lines (227 loc) · 6.9 KB
/
SQL_Sales_Project.sql
File metadata and controls
259 lines (227 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
--Which database is used for performing the sales analysis?
/* 1. Use existing database */
USE sales_analysis_db;
GO
--How can we safely reset the database before recreating tables?
/* 2. Drop tables only if they exist (safe reset) */
DROP TABLE IF EXISTS Orders;
DROP TABLE IF EXISTS Products;
DROP TABLE IF EXISTS Customers;
GO
--What is the structure of the sales database, and how are customers, products, and orders related?
/* 3. Recreate tables */
CREATE TABLE Customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(100),
segment VARCHAR(50),
city VARCHAR(50),
state VARCHAR(50)
);
CREATE TABLE Products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
category VARCHAR(50),
sub_category VARCHAR(50)
);
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
order_date DATE,
customer_id INT,
product_id INT,
sales DECIMAL(10,2),
quantity INT,
discount DECIMAL(4,2),
profit DECIMAL(10,2),
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id),
FOREIGN KEY (product_id) REFERENCES Products(product_id)
);
GO
--Have all required tables been created successfully in the database?
SELECT name FROM sys.tables;
USE sales_analysis_db;
GO
--What sample data is available for customers, products, and sales transactions?
INSERT INTO Customers VALUES
(1, 'Rohit Sharma', 'Cricketer', 'Mumbai', 'Maharashtra'),
(2, 'Harmanpreeet Kaur', 'Cricketer', 'Punjab', 'Punjab'),
(3, 'Virat Kohli', 'Cricketer', 'Delhi', 'Delhi'),
(4, 'Kriti Sanon', 'Actress', 'Mumbai', 'Maharashtra'),
(5, 'Sanman Kadam', 'Corporate', 'Mumbai', 'Maharashtra');
INSERT INTO Products VALUES
(101, 'Laptop', 'Technology', 'Computers'),
(102, 'Printer', 'Technology', 'Accessories'),
(103, 'Office Chair', 'Furniture', 'Chairs'),
(104, 'Notebook', 'Office Supplies', 'Paper'),
(105, 'Smartphone', 'Technology', 'Mobiles');
INSERT INTO Orders VALUES
(1001, '2024-01-10', 1, 101, 55000, 1, 0.10, 8000),
(1002, '2024-01-15', 2, 103, 12000, 2, 0.05, 2500),
(1003, '2024-02-05', 3, 104, 500, 10, 0.00, 200),
(1004, '2024-02-20', 4, 102, 8000, 1, 0.15, 1000),
(1005, '2024-03-01', 5, 105, 30000, 1, 0.20, -2000);
--What does the raw data look like in each table?
SELECT * FROM Customers;
SELECT * FROM Products;
SELECT * FROM Orders;
--What is the total sales generated across all orders?
SELECT
SUM(sales) AS Total_Sales
FROM Orders;
--What is the overall profit earned from all sales?
SELECT
SUM(profit) AS Total_Profit
FROM Orders;
--How many unique orders have been placed?
SELECT
COUNT(DISTINCT order_id) AS Total_Orders
FROM Orders;
--What is the average value of an order?
SELECT
AVG(sales) AS Avg_Order_Value
FROM Orders;
--What is the total quantity of products sold?
SELECT
SUM(quantity) AS Total_Quantity
FROM Orders;
--What is the overall profit margin percentage of the business?
SELECT
(SUM(profit) / SUM(sales)) * 100 AS Profit_Margin_Percentage
FROM Orders;
--Which product categories generate the highest sales?
SELECT
p.category,
SUM(o.sales) AS Total_Sales
FROM Orders o
JOIN Products p ON o.product_id = p.product_id
GROUP BY p.category
ORDER BY Total_Sales DESC;
--Which product categories are the most profitable?
--How do products rank based on total sales performance?
SELECT
p.category,
SUM(o.profit) AS Total_Profit
FROM Orders o
JOIN Products p ON o.product_id = p.product_id
GROUP BY p.category
ORDER BY Total_Profit DESC;
--Which customers contribute the most to total sales?
SELECT
c.customer_name,
SUM(o.sales) AS Total_Sales
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
GROUP BY c.customer_name
ORDER BY Total_Sales DESC;
--Which states generate the highest sales revenue?
SELECT
c.state,
SUM(o.sales) AS Total_Sales
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
GROUP BY c.state
ORDER BY Total_Sales DESC;
--How do sales trend month by month over time?
SELECT
YEAR(order_date) AS Year,
MONTH(order_date) AS Month,
SUM(sales) AS Monthly_Sales
FROM Orders
GROUP BY YEAR(order_date), MONTH(order_date)
ORDER BY Year, Month;
--How does profit change across different months?
SELECT
YEAR(order_date) AS Year,
MONTH(order_date) AS Month,
SUM(profit) AS Monthly_Profit
FROM Orders
GROUP BY YEAR(order_date), MONTH(order_date)
ORDER BY Year, Month;
SELECT
p.product_name,
SUM(o.sales) AS Total_Sales,
RANK() OVER (ORDER BY SUM(o.sales) DESC) AS Sales_Rank
FROM Orders o
JOIN Products p ON o.product_id = p.product_id
GROUP BY p.product_name;
SELECT
c.customer_name,
SUM(o.sales) AS Customer_Sales,
ROUND(
SUM(o.sales) * 100.0 / SUM(SUM(o.sales)) OVER (), 2
) AS Contribution_Percentage
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
GROUP BY c.customer_name;
--How do different discount levels impact average profit?
SELECT
discount,
AVG(profit) AS Avg_Profit
FROM Orders
GROUP BY discount
ORDER BY discount;
--Are there any orders with missing sales or profit values?
--Are there any orders without matching customer records?
SELECT *
FROM Orders
WHERE sales IS NULL OR profit IS NULL;
SELECT *
FROM Orders
WHERE profit < 0;
GO
CREATE OR ALTER VIEW vw_sales_summary AS
SELECT
c.state,
p.category,
SUM(o.sales) AS Total_Sales,
SUM(o.profit) AS Total_Profit
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
JOIN Products p ON o.product_id = p.product_id
GROUP BY c.state, p.category;
GO
SELECT * FROM vw_sales_summary;
GO
--Can we create a summarized view of sales and profit by state and category?
CREATE OR ALTER PROCEDURE sp_sales_by_state
AS
BEGIN
SELECT
c.state,
SUM(o.sales) AS Total_Sales,
SUM(o.profit) AS Total_Profit
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
GROUP BY c.state
ORDER BY Total_Sales DESC;
END;
GO
--How can we retrieve total sales and profit for each state dynamically?
EXEC sp_sales_by_state;
GO
--Who are the top N customers based on total sales?
CREATE OR ALTER PROCEDURE sp_top_customers
@TopN INT
AS
BEGIN
SELECT TOP (@TopN)
c.customer_name,
SUM(o.sales) AS Total_Sales
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
GROUP BY c.customer_name
ORDER BY Total_Sales DESC;
END;
GO
--How can query performance be improved for frequent joins and filters?
EXEC sp_top_customers 3;
CREATE INDEX idx_orders_customer_id ON Orders(customer_id);
CREATE INDEX idx_orders_product_id ON Orders(product_id);
CREATE INDEX idx_orders_order_date ON Orders(order_date);
SELECT *
FROM Orders o
LEFT JOIN Customers c
ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;
SELECT *
FROM Orders
WHERE discount > 0.15 AND profit < 0;