-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecommerce_analysis.sql
More file actions
271 lines (246 loc) · 8.69 KB
/
Copy pathecommerce_analysis.sql
File metadata and controls
271 lines (246 loc) · 8.69 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
260
261
262
263
264
265
266
267
268
269
270
-- ====================================
-- SQL E-Commerce Analysis Project
-- Author: Devesh Shukla
-- Tool: MySQL Workbench
-- ====================================
-- 1. Database Creation
CREATE DATABASE sql_business_project;
use sql_business_project;
-- 2. Table Creation
CREATE TABLE customers (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(100),
email VARCHAR(100),
signup_date DATE,
country VARCHAR(50)
);
CREATE TABLE products (
product_id INT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100),
category VARCHAR(50),
price DECIMAL(10,2)
);
CREATE TABLE orders (
order_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT,
order_date DATE,
order_status VARCHAR(50),
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id)
);
CREATE TABLE order_items (
order_item_id INT PRIMARY KEY AUTO_INCREMENT,
order_id INT,
product_id INT,
quantity INT,
FOREIGN KEY (order_id)
REFERENCES orders(order_id),
FOREIGN KEY (product_id)
REFERENCES products(product_id)
);
-- 3. Data Insertion
INSERT INTO products (product_name, category, price) VALUES
('Laptop','Electronics',800),
('Headphones','Electronics',120),
('Smartphone','Electronics',700),
('T-shirt','Clothing',25),
('Jeans','Clothing',60),
('Sneakers','Footwear',90),
('Backpack','Accessories',50),
('Watch','Accessories',150),
('Coffee Maker','Home Appliances',80),
('Blender','Home Appliances',70);
SELECT * FROM products;
INSERT INTO customers (customer_name, email, signup_date, country)
SELECT
CONCAT('Customer_', FLOOR(RAND()*10000)),
CONCAT('user', FLOOR(RAND()*100000), '@email.com'),
DATE_SUB(CURDATE(), INTERVAL FLOOR(RAND()*365) DAY),
ELT(FLOOR(1 + RAND()*5),'USA','India','UK','Canada','Germany')
FROM
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) t1,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) t2,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) t3;
SELECT COUNT(*) FROM customers;
INSERT INTO orders (customer_id, order_date, order_status)
SELECT
FLOOR(1 + RAND()*1000),
DATE_SUB(CURDATE(), INTERVAL FLOOR(RAND()*365) DAY),
ELT(FLOOR(1 + RAND()*3),'Completed','Pending','Cancelled')
FROM
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) a,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) b,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) c,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) d;
SELECT COUNT(*) FROM orders;
INSERT INTO order_items (order_id, product_id, quantity)
SELECT
FLOOR(1 + RAND()*5000),
FLOOR(1 + RAND()*10),
FLOOR(1 + RAND()*5)
FROM
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) a,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) b,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) c,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8) d;
SELECT COUNT(*) FROM order_items;
-- =====================================================
-- BUSINESS QUERIES
-- =====================================================
-- Business Question:
-- What is the total revenue generated by the company from all orders?
SELECT
SUM(p.price * oi.quantity) AS total_revenue
FROM order_items oi
JOIN products p
ON oi.product_id = p.product_id;
-- Explanation:
-- This query joins the products and order_items tables to calculate
-- the total revenue by multiplying product price with quantity sold.
-- Business Insight:
-- This metric shows the overall financial performance of the business.
-- Tracking total revenue helps management evaluate growth trends
-- and make strategic decisions about pricing, promotions, and inventory.
-- Business Question:
-- Which products generate the highest revenue for the business?
SELECT
p.product_name,
SUM(oi.quantity) AS total_units_sold
FROM order_items oi
JOIN products p
ON oi.product_id = p.product_id
GROUP BY p.product_name
ORDER BY total_units_sold DESC
LIMIT 5;
-- Explanation:
-- This query calculates the total revenue generated by each product
-- by multiplying price with quantity sold and grouping by product name.
-- Business Insight:
-- Understanding top-performing products helps businesses focus on
-- inventory planning, marketing efforts, and supply chain optimization
-- to maximize profit.
-- Business Question:
-- Which customers contribute the most revenue to the company?
SELECT
c.customer_name,
SUM(p.price * oi.quantity) AS total_spent
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id
JOIN order_items oi
ON o.order_id = oi.order_id
JOIN products p
ON oi.product_id = p.product_id
GROUP BY c.customer_name
ORDER BY total_spent DESC
LIMIT 10;
-- Explanation:
-- This query joins customers, orders, order_items, and products tables
-- to calculate the total amount spent by each customer.
-- Business Insight:
-- A small percentage of customers usually generate a large portion
-- of revenue (Pareto Principle). Identifying these high-value customers
-- helps businesses design loyalty programs and targeted marketing campaigns.
-- Business Question:
-- How does revenue change month by month?
SELECT
DATE_FORMAT(o.order_date,'%Y-%m') AS month,
SUM(p.price * oi.quantity) AS revenue
FROM orders o
JOIN order_items oi
ON o.order_id = oi.order_id
JOIN products p
ON oi.product_id = p.product_id
GROUP BY month
ORDER BY month;
-- Explanation:
-- This query groups sales data by month using order dates and
-- calculates the total revenue for each month.
-- Business Insight:
-- Monthly revenue trends help identify seasonality patterns,
-- sales growth, and potential slow periods. Businesses can use
-- this information to plan promotions, discounts, and marketing
-- strategies during low-performing months.
-- Business Question:
-- How can we categorize customers based on their total spending?
SELECT * FROM customers;
SELECT
c.customer_id,
c.customer_name,
SUM(p.price * oi.quantity) AS total_spent,
CASE
WHEN SUM(p.price * oi.quantity) > 500 THEN 'High Value Customer'
WHEN SUM(p.price * oi.quantity) BETWEEN 200 AND 500 THEN 'Medium Value Customer'
ELSE 'Low Value Customer'
END AS customer_segment
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id
JOIN order_items oi
ON o.order_id = oi.order_id
JOIN products p
ON oi.product_id = p.product_id
GROUP BY
c.customer_id, c.customer_name;
-- Explanation:
-- This query calculates total spending for each customer and
-- classifies them into segments using CASE WHEN logic.
-- Business Insight:
-- Customer segmentation helps businesses identify their most valuable
-- customers and design personalized marketing campaigns.
-- Business Question:
-- Which customers generate the highest lifetime revenue?
WITH customer_spending AS (
SELECT
c.customer_id,
c.customer_name,
SUM(p.price * oi.quantity) AS lifetime_value
FROM customers c
JOIN orders o
ON c.customer_id = o.customer_id
JOIN order_items oi
ON o.order_id = oi.order_id
JOIN products p
ON oi.product_id = p.product_id
GROUP BY
c.customer_id, c.customer_name
)
SELECT *
FROM customer_spending
ORDER BY lifetime_value DESC;
-- Explanation:
-- This query calculates the total revenue generated by each customer
-- across all their orders using a Common Table Expression (CTE).
-- Business Insight:
-- Identifying high CLV customers helps businesses focus on retention,
-- loyalty programs, and premium services.
-- Business Question:
-- Which products generate the highest revenue?
SELECT
p.product_id,
p.product_name,
SUM(p.price * oi.quantity) AS total_revenue,
RANK() OVER (
ORDER BY SUM(p.price * oi.quantity) DESC
) AS product_rank
FROM products p
JOIN order_items oi
ON p.product_id = oi.product_id
GROUP BY
p.product_id, p.product_name;
-- Explanation:
-- This query calculates total revenue generated by each product
-- and ranks them using a window function.
-- Business Insight:
-- Identifying top-selling products helps businesses focus on
-- inventory planning, marketing campaigns, and pricing strategy.