Repository navigation
Expand file tree
/
Copy pathProject_customerbehavior.sql
More file actions
73 lines (53 loc) · 2.17 KB
/
Copy pathProject_customerbehavior.sql
File metadata and controls
73 lines (53 loc) · 2.17 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
use customer_behavior;
SELECT * FROM customer;
-- 1. What is the total revenue generated by male vs. female customers?
SELECT gender, SUM(purchase_amount) as revenue
FROM customer
GROUP BY gender;
-- 2. Which customers used a discount 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);
-- 3. Which are the top 5 products with the highest average rating?
SELECT TOP 5 item_purchased, ROUND(AVG(CAST(review_rating AS DECIMAL(10,2))),2) AS [Average Product Rating]
FROM customer
GROUP BY item_purchased
ORDER BY AVG(CAST(review_rating AS DECIMAL(10,2))) DESC;
-- 4. Compare the average purchase Amounts between standard and Express Shipping?
SELECT shipping_type, ROUND(AVG(purchase_amount), 2)
FROM customer
WHERE shipping_type in ('standard', 'Express')
GROUP BY shipping_type;
--5. Do subscribed customers spend more? Compare average spend and total revenue between subscribers and non-subscribers ?
SELECT subscription_status,
COUNT(customer_id) as total_customers,
ROUND(AVG(purchase_amount), 2) as avg_spend,
ROUND(SUM(purchase_amount), 2) as total_revenue
FROM customer
GROUP BY subscription_status
ORDER BY avg_spend, total_revenue DESC;
-- 6. Which 5 products have the highest percentage of purchases with discounts applied?
-- (which products heavily depend on discounts to sell)
SELECT Top 5 item_purchased,
ROUND(100 * SUM(CASE WHEN discount_applied = 'Yes' THEN 1 ELSE 0 END) / COUNT(*) , 2) as discount_rate
FROM customer
GROUP BY item_purchased
ORDER BY discount_rate DESC;
-- 7. Segment customers into New, Returning, and Loyal based on their total number of previous purchases, 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 customer_segment;