-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_analysis.sql
More file actions
133 lines (96 loc) · 3.2 KB
/
Data_analysis.sql
File metadata and controls
133 lines (96 loc) · 3.2 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
Select *
from customer
Limit 20;
-- Total revenue generated by male and female
select gender , sum(purchase_amount) as Revenue
from customer
group by gender;
-- which customers used a discont but still spent more than the average purchase amount
select customer_id ,purchase_amount
from customer
where discount_applied ="Yes" and purchase_amount >= (select avg(purchase_amount) from customer);
-- which are the top 5 products with the highest average review rating?
select item_purchased , Round(avg(review_rating),2) as `Average Product Rating`
from customer
group by item_purchased
order by 2 desc
limit 5;
-- compare the avg purchase amount between standerd and express shipping
SELECT
shipping_type,
ROUND(AVG(purchase_amount), 2) AS avg_purchase
FROM customer
WHERE shipping_type IN ('Express', 'Standard')
GROUP BY shipping_type;
-- another method
WITH Express_Type AS (
SELECT ROUND(AVG(purchase_amount), 2) AS express_avg
FROM customer
WHERE shipping_type = 'Express'
),
Standard_Type AS (
SELECT ROUND(AVG(purchase_amount), 2) AS standard_avg
FROM customer
WHERE shipping_type = 'Standard'
)
SELECT
s.standard_avg,
e.express_avg
FROM Standard_Type s
CROSS JOIN Express_Type e;
-- do subscribed customers spend more? compare average spend and total revenue between subscribed and non subscreibed
select subscription_status ,count(customer_id),
Round(avg(purchase_amount),2) as avg_spend,
sum(purchase_amount) as total_revenue
from customer
group by 1
order by 3,2 desc;
-- which 5 product has the highest percentage of purchase with discount applied?
select item_purchased,
Round(sum(case when discount_applied = 'Yes' then 1 else 0 end)/count(*) *100,2) as `discount_rate %`
from customer
group by item_purchased
order by 2 desc
limit 5;
-- segment cust into new , returning and loyal based on their total number of pervious purchase , and show the count of each segment.
WITH customer_type AS (
SELECT customer_id, previous_purchases,
CASE
WHEN previous_purchases = 1 THEN 'New'
WHEN previous_purchases BETWEEN 2 AND 10 THEN 'Returning'
ELSE 'Loyal'
END AS Customer_Segment
FROM customer
)
select customer_Segment , count(*) as `Number of Customers`
from customer_type
group by 1;
-- what are the top 3 most purchased products with in each category
WITH item_counts AS (
SELECT
category,
item_purchased,
COUNT(customer_id) AS total_orders,
ROW_NUMBER() OVER (
PARTITION BY category
ORDER BY COUNT(customer_id) DESC
) AS item_rank
FROM customer
GROUP BY category, item_purchased
)
SELECT item_rank, category, item_purchased, total_orders
FROM item_counts
WHERE item_rank <= 3;
-- Are customers who are repeat buyers (more than 5 previous purchases ) also likely to subscribe ?
select count(customer_id)
from customer
where previous_purchases >= 5 and subscription_status = "Yes";
select subscription_status , count(customer_id) as `Repeat Buyers`
from customer
where previous_purchases > 5
group by 1;
-- revenue by age group
select age_group , sum(purchase_amount) as `Total Revenue`
from customer
group by 1
order by 2 desc;