-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
149 lines (118 loc) · 3.93 KB
/
queries.sql
File metadata and controls
149 lines (118 loc) · 3.93 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
/* =========================================
PROJECT: Customer & Sales Analysis
DATABASE: MySQL
========================================= */
/* -----------------------------------------
1. View Raw Tables
----------------------------------------- */
SELECT * FROM customers;
SELECT * FROM products;
SELECT * FROM orders;
/* -----------------------------------------
2. Basic Metrics
----------------------------------------- */
-- Total number of customers
SELECT COUNT(*) AS total_customers
FROM customers;
-- Total number of orders
SELECT COUNT(*) AS total_orders
FROM orders;
/* -----------------------------------------
3. Orders per Customer (LEFT JOIN)
----------------------------------------- */
SELECT
customers.name,
COUNT(orders.order_id) AS total_orders
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
GROUP BY customers.name;
/* -----------------------------------------
4. Customer Category by Order Frequency
----------------------------------------- */
SELECT
customers.name,
COUNT(orders.order_id) AS order_quantity,
CASE
WHEN COUNT(orders.order_id) >= 2 THEN 'Frequent Buyer'
WHEN COUNT(orders.order_id) = 1 THEN 'Occasional Buyer'
ELSE 'No Orders'
END AS customer_category
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
GROUP BY customers.name;
/* -----------------------------------------
5. Revenue by Product
----------------------------------------- */
SELECT
products.product_name,
SUM(orders.quantity) AS total_quantity_sold,
SUM(orders.quantity * products.price) AS total_revenue
FROM orders
JOIN products
ON orders.product_id = products.product_id
GROUP BY products.product_name;
/* -----------------------------------------
6. High Revenue Products (HAVING)
----------------------------------------- */
SELECT
products.product_name,
SUM(orders.quantity * products.price) AS total_revenue
FROM orders
JOIN products
ON orders.product_id = products.product_id
GROUP BY products.product_name
HAVING SUM(orders.quantity * products.price) > 50000;
/* -----------------------------------------
7. Total Spending per Customer
----------------------------------------- */
SELECT
customers.name,
SUM(products.price * orders.quantity) AS total_spending
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
LEFT JOIN products
ON products.product_id = orders.product_id
GROUP BY customers.name;
/* -----------------------------------------
8. Customer Spending Category
----------------------------------------- */
SELECT
customers.name,
SUM(products.price * orders.quantity) AS total_spending,
CASE
WHEN SUM(products.price * orders.quantity) > 50000 THEN 'High Spender'
WHEN SUM(products.price * orders.quantity) BETWEEN 20000 AND 50000 THEN 'Medium Spender'
ELSE 'Low Spender'
END AS spending_category
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
LEFT JOIN products
ON products.product_id = orders.product_id
GROUP BY customers.name;
/* -----------------------------------------
9. Frequent AND High-Value Customers
----------------------------------------- */
SELECT
customers.name,
COUNT(orders.order_id) AS order_quantity,
SUM(products.price * orders.quantity) AS total_spending,
CASE
WHEN COUNT(orders.order_id) >= 2 THEN 'Frequent Buyer'
WHEN COUNT(orders.order_id) = 1 THEN 'Occasional Buyer'
ELSE 'No Orders'
END AS customer_category
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
LEFT JOIN products
ON products.product_id = orders.product_id
GROUP BY customers.name
HAVING COUNT(orders.order_id) >= 2
AND SUM(products.price * orders.quantity) > 50000;
/* -----------------------------------------
END OF ANALYSIS QUERIES
----------------------------------------- */