-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlproject.sql
More file actions
429 lines (321 loc) · 10.4 KB
/
sqlproject.sql
File metadata and controls
429 lines (321 loc) · 10.4 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
USE database;
CREATE TABLE sales_store (
transaction_id VARCHAR(15),
customer_id VARCHAR(15),
customer_name VARCHAR(30),
customer_age INT,
gender VARCHAR(15),
product_id VARCHAR(15),
product_name VARCHAR(15),
product_category VARCHAR(15),
quantiy INT,
prce FLOAT,
payment_mode VARCHAR(15),
purchase_date DATE,
time_of_purchase TIME,
status VARCHAR(15)
);
SELECT * FROM sales_store
SET DATEFORMAT dmy
BULK INSERT sales_store
FROM 'source file'
WITH (
FIRSTROW=2,
FIELDTERMINATOR=',',
ROWTERMINATOR='\n'
);
--YYYY-MM-DD
--Data Cleaning
SELECT * FROM sales_store
SELECT * INTO sales FROM sales_store
SELECT * FROM sales
--Data Cleaning
--Step 1:- To check for Duplicate
SELECT transaction_id,COUNT(*)
FROM sales
GROUP BY transaction_id
HAVING COUNT(transaction_id) >1
TXN240646
TXN342128
TXN855235
TXN981773
WITH CTE AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY transaction_id ORDER BY transaction_id) AS Row_Num
FROM sales
)
--DELETE FROM CTE
--WHERE Row_Num=2
SELECT * FROM CTE
WHERE transaction_id IN ('TXN240646','TXN342128','TXN855235','TXN981773')
--Step 2 :- Correction of Headers
SELECT * FROM sales
EXEC sp_rename'sales.quantiy','quantity','COLUMN'
EXEC sp_rename'sales.prce','price','COLUMN'
--Step 3 :- To check Datatype
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='sales'
--Step 4 :- To Check Null Values
--to check null count
DECLARE @SQL NVARCHAR(MAX) = '';
SELECT @SQL = STRING_AGG(
'SELECT ''' + COLUMN_NAME + ''' AS ColumnName,
COUNT(*) AS NullCount
FROM ' + QUOTENAME(TABLE_SCHEMA) + '.sales
WHERE ' + QUOTENAME(COLUMN_NAME) + ' IS NULL',
' UNION ALL '
)
WITHIN GROUP (ORDER BY COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'sales';
-- Execute the dynamic SQL
EXEC sp_executesql @SQL;
--treating null values
SELECT *
FROM sales
WHERE transaction_id IS NULL
OR
customer_id IS NULL
OR
customer_name IS NULL
OR
customer_age IS NULL
OR
gender IS NULL
OR
product_id IS NULL
OR
product_name IS NULL
OR
product_category IS NULL
OR
quantity IS NULL
or
payment_mode is null
or
purchase_date is null
or
status is null
or
price is null
DELETE FROM sales
WHERE transaction_id IS NULL
SELECT * FROM sales
Where Customer_name='Ehsaan Ram'
UPDATE sales
SET customer_id='CUST9494'
WHERE transaction_id='TXN977900'
SELECT * FROM sales
Where Customer_name='Damini Raju'
UPDATE sales
SET customer_id='CUST1401'
WHERE transaction_id='TXN985663'
SELECT * FROM sales
Where Customer_id='CUST1003'
UPDATE sales
SET customer_name='Mahika Saini',customer_age=35,gender='Male'
WHERE transaction_id='TXN432798'
SELECT * FROM sales
--Step 5:- Data Cleaning
SELECT DISTINCT gender
FROM sales
UPDATE sales
SET gender='M'
WHERE gender='Male'
UPDATE sales
SET gender='F'
WHERE gender='Female'
SELECT DISTINCT payment_mode
FROM sales
UPDATE sales
SET payment_mode='Credit Card'
WHERE payment_mode='CC'
-----------------------------------------------------------------------------------------------------------
--Data Analysis--
--🔥 1. What are the top 5 most selling products by quantity?
SELECT * FROM sales
SELECT DISTINCT status
from sales
SELECT TOP 5 product_name, SUM(quantity) AS total_quantity_sold
FROM sales
WHERE status='delivered'
GROUP BY product_name
ORDER BY total_quantity_sold DESC
--Business Problem: We don't know which products are most in demand.
--Business Impact: Helps prioritize stock and boost sales through targeted promotions.
-----------------------------------------------------------------------------------------------------------
--📉 2. Which products are most frequently cancelled?
SELECT TOP 5 product_name, COUNT(*) AS total_cancelled
FROM sales
WHERE status='cancelled'
GROUP BY product_name
ORDER BY total_cancelled DESC
--Business Problem: Frequent cancellations affect revenue and customer trust.
--Business Impact: Identify poor-performing products to improve quality or remove from catalog.
-----------------------------------------------------------------------------------------------------------
--🕒 3. What time of the day has the highest number of purchases?
select * from sales
SELECT
CASE
WHEN DATEPART(HOUR,time_of_purchase) BETWEEN 0 AND 5 THEN 'NIGHT'
WHEN DATEPART(HOUR,time_of_purchase) BETWEEN 6 AND 11 THEN 'MORNING'
WHEN DATEPART(HOUR,time_of_purchase) BETWEEN 12 AND 17 THEN 'AFTERNOON'
WHEN DATEPART(HOUR,time_of_purchase) BETWEEN 18 AND 23 THEN 'EVENING'
END AS time_of_day,
COUNT(*) AS total_orders
FROM sales
GROUP BY
CASE
WHEN DATEPART(HOUR,time_of_purchase) BETWEEN 0 AND 5 THEN 'NIGHT'
WHEN DATEPART(HOUR,time_of_purchase) BETWEEN 6 AND 11 THEN 'MORNING'
WHEN DATEPART(HOUR,time_of_purchase) BETWEEN 12 AND 17 THEN 'AFTERNOON'
WHEN DATEPART(HOUR,time_of_purchase) BETWEEN 18 AND 23 THEN 'EVENING'
END
ORDER BY total_orders DESC
---------------------------------------------------------------------------------------------
SELECT
DATEPART(HOUR,time_of_purchase) AS Peak_time,
COUNT(*) AS Total_orders
FROM sales
GROUP BY DATEPART(HOUR,time_of_purchase)
ORDER BY Peak_time
--Business Problem Solved: Find peak sales times.
--Business Impact: Optimize staffing, promotions, and server loads.
-----------------------------------------------------------------------------------------------------------
--👥 4. Who are the top 5 highest spending customers?
SELECT * FROM sales
SELECT TOP 5 customer_name,
FORMAT(SUM(price*quantity),'C0','en-IN') AS total_spend
FROM sales
GROUP BY customer_name
ORDER BY SUM(price*quantity) DESC
--Business Problem Solved: Identify VIP customers.
--Business Impact: Personalized offers, loyalty rewards, and retention.
-----------------------------------------------------------------------------------------------------------
--🛍️ 5. Which product categories generate the highest revenue?
SELECT * FROM sales
SELECT
product_category,
FORMAT(SUM(price*quantity),'C0','en-IN') AS Revenue
FROM sales
GROUP BY product_category
ORDER BY SUM(price*quantity) DESC
--Business Problem Solved: Identify top-performing product categories.
--Business Impact: Refine product strategy, supply chain, and promotions.
--allowing the business to invest more in high-margin or high-demand categories.
-----------------------------------------------------------------------------------------------------------
--🔄 6. What is the return/cancellation rate per product category?
SELECT * FROM sales
--cancellation
SELECT product_category,
FORMAT(COUNT(CASE WHEN status='cancelled' THEN 1 END)*100.0/COUNT(*),'N3')+' %' AS cancelled_percent
FROM sales
GROUP BY product_category
ORDER BY cancelled_percent DESC
--Return
SELECT product_category,
FORMAT(COUNT(CASE WHEN status='returned' THEN 1 END)*100.0/COUNT(*),'N3')+' %' AS returned_percent
FROM sales
GROUP BY product_category
ORDER BY returned_percent DESC
--Business Problem Solved: Monitor dissatisfaction trends per category.
---Business Impact: Reduce returns, improve product descriptions/expectations.
--Helps identify and fix product or logistics issues.
-----------------------------------------------------------------------------------------------------------
--💳 7. What is the most preferred payment mode?
SELECT * FROM sales
SELECT payment_mode, COUNT(payment_mode) AS total_count
FROM sales
GROUP BY payment_mode
ORDER BY total_count desc
--Business Problem Solved: Know which payment options customers prefer.
--Business Impact: Streamline payment processing, prioritize popular modes.
-----------------------------------------------------------------------------------------------------------
--🧓 8. How does age group affect purchasing behavior?
SELECT * FROM sales
--SELECT MIN(customer_age) ,MAX(customer_age)
--from sales
SELECT
CASE
WHEN customer_age BETWEEN 18 AND 25 THEN '18-25'
WHEN customer_age BETWEEN 26 AND 35 THEN '26-35'
WHEN customer_age BETWEEN 36 AND 50 THEN '36-50'
ELSE '51+'
END AS customer_age,
FORMAT(SUM(price*quantity),'C0','en-IN') AS total_purchase
FROM sales
GROUP BY CASE
WHEN customer_age BETWEEN 18 AND 25 THEN '18-25'
WHEN customer_age BETWEEN 26 AND 35 THEN '26-35'
WHEN customer_age BETWEEN 36 AND 50 THEN '36-50'
ELSE '51+'
END
ORDER BY SUM(price*quantity) DESC
--Business Problem Solved: Understand customer demographics.
--Business Impact: Targeted marketing and product recommendations by age group.
-----------------------------------------------------------------------------------------------------------
--🔁 9. What’s the monthly sales trend?
SELECT * FROM sales
--Method 1
SELECT
FORMAT(purchase_date,'yyyy-MM') AS Month_Year,
FORMAT(SUM(price*quantity),'C0','en-IN') AS total_sales,
SUM(quantity) AS total_quantity
FROM sales
GROUP BY FORMAT(purchase_date,'yyyy-MM')
--Method 2
SELECT * FROM sales
SELECT
--YEAR(purchase_date) AS Years,
MONTH(purchase_date) AS Months,
FORMAT(SUM(price*quantity),'C0','en-IN') AS total_sales,
SUM(quantity) AS total_quantity
FROM sales
GROUP BY MONTH(purchase_date)
ORDER BY Months
--2023 1 ₹ 46,28,608
--2024 1 ₹ 3,39,442
SELECT(4628608+339442)--4968050
--Business Problem: Sales fluctuations go unnoticed.
--Business Impact: Plan inventory and marketing according to seasonal trends.
SELECT * FROM sales
SELECT
FORMAT(purchase_date,'yyyy-MM') AS Purchased_Month_Year,
SUM(price*quantity) AS totalsales,
SUM(quantity) AS totalquantity
FROM sales
GROUP BY FORMAT(purchase_date,'yyyy-MM')
ORDER BY Purchased_Month_Year ASC
-----------------------------------------------------------------------------------------------------------
SELECT
--YEAR(purchase_date) AS years,
MONTH(purchase_date) as months,
SUM(price*quantity) AS totalsales
FROM sales
GROUP BY MONTH(purchase_date)
--YEAR(purchase_date)
ORDER BY months ASC
--Business Problem: Sales fluctuations go unnoticed.
--Business Impact: Plan inventory and marketing according to seasonal trends.
-----------------------------------------------------------------------------------------------------------
--🔎 10. Are certain genders buying more specific product categories?
SELECT * from sales
--Method 1
SELECT gender,product_category,COUNT(product_category) AS total_purchase
FROM sales
GROUP BY gender,product_category
ORDER BY gender
--Method 2
SELECT *
FROM (
SELECT gender,product_category
FROM sales
) AS source_table
PIVOT (
COUNT(gender)
FOR gender IN ([Male],[Female])
) AS pivot_table
ORDER BY product_category
--Business Problem Solved: Gender-based product preferences.
--Business Impact: Personalized ads, gender-focused campaigns.