-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer_behavior_sql_queries.sql
More file actions
87 lines (72 loc) · 3.35 KB
/
Copy pathcustomer_behavior_sql_queries.sql
File metadata and controls
87 lines (72 loc) · 3.35 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
--1. Каков общий доход (revenue), сгенерированный мужчинами и женщинами?
select gender, SUM(purchase_amount) as revenue
from customer
group by gender
--2. Какие клиенты использовали скидку, но при этом потратили больше среднего чека?
select customer_id, purchase_amount
from customer
where discount_applied = 'Yes' and purchase_amount >= (select AVG(purchase_amount) from customer)
-- 3. Какие 5 товаров имеют самый высокий средний рейтинг?
select item_purchased, round(avg(review_rating::numeric),2) as "Average Product Rating"
from customer
group by item_purchased
order by avg(review_rating) desc
limit 5
--4. Как различается средний чек между стандартной и экспресс-доставкой?
select shipping_type,
ROUND(AVG(purchase_amount),2)
from customer
where shipping_type in ('Standard','Express')
group by shipping_type;
--5. Тратят ли подписанные пользователи больше? (сравнение среднего чека и общей выручки)
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 total_revenue,avg_spend DESC;
--6. У каких 5 товаров наибольшая доля покупок со скидками?
SELECT item_purchased,
ROUND(100.0 * 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
LIMIT 5;
--7. Как распределяются клиенты по сегментам (новые, возвращающиеся, лояльные) в зависимости от количества предыдущих покупок?
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;
--8. Какие 3 самых популярных товара в каждой категории?
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;
--9. Склонны ли постоянные покупатели (более 5 покупок) оформлять подписку?
SELECT subscription_status,
COUNT(customer_id) AS repeat_buyers
FROM customer
WHERE previous_purchases > 5
GROUP BY subscription_status;
--10. Какой вклад в выручку вносит каждая возрастная группа?
SELECT
age_group,
SUM(purchase_amount) AS total_revenue
FROM customer
GROUP BY age_group
ORDER BY total_revenue desc;