-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday9_sql_practice.sql
More file actions
207 lines (168 loc) · 4.74 KB
/
day9_sql_practice.sql
File metadata and controls
207 lines (168 loc) · 4.74 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
-- ============================================================
-- 📘 DAY 9: SQL CTE (COMMON TABLE EXPRESSION) PRACTICE
-- ============================================================
-- ============================
-- 📌 1) CTE: Mode-wise Max & Total Amount
-- ============================
WITH temp_table AS (
SELECT mode,
MAX(amount) AS Max_Amount,
SUM(amount) AS Total_Amount
FROM accounts
GROUP BY mode
)
SELECT a.*, t.Max_Amount, t.Total_Amount
FROM accounts a
JOIN temp_table t ON t.mode = a.mode;
-- ============================
-- 📌 2) CTE: Customer-wise Total Amount
-- ============================
WITH temp_table AS (
SELECT customer_id, SUM(amount) AS Total_Amount
FROM accounts
GROUP BY customer_id
)
SELECT c.customer_name, c.city, t.Total_Amount
FROM customers c
JOIN temp_table t ON c.customer_id = t.customer_id;
-- ============================
-- 📌 3) CTE: Average Amount (mode-wise) + Filter
-- ============================
WITH temp_table AS (
SELECT mode, ROUND(AVG(amount)) AS Avg_Amount
FROM accounts
GROUP BY mode
)
SELECT a.account_id, a.customer_id, a.amount, a.mode, t.Avg_Amount
FROM accounts a
JOIN temp_table t ON a.mode = t.mode
WHERE a.amount > t.Avg_Amount;
-- ============================
-- 📌 4) CTE: Mode-wise Transaction Count (>2)
-- ============================
WITH temp_table AS (
SELECT mode, COUNT(*) AS total_transactions
FROM accounts
GROUP BY mode
)
SELECT a.mode, t.total_transactions
FROM accounts a
JOIN temp_table t ON a.mode = t.mode
WHERE t.total_transactions > 2;
-- ============================
-- 📌 5) CTE: Customers Spending > 1000
-- ============================
WITH temp_table AS (
SELECT customer_id, SUM(amount) AS total_spending
FROM accounts
GROUP BY customer_id
)
SELECT c.customer_name, t.total_spending
FROM customers c
JOIN temp_table t ON c.customer_id = t.customer_id
WHERE total_spending > 1000;
-- ============================
-- 📌 6) CTE: Mode-wise Average Amount > 600
-- ============================
WITH temp_table AS (
SELECT mode, AVG(amount) AS avg_amount
FROM accounts
GROUP BY mode
)
SELECT mode, avg_amount
FROM temp_table
WHERE avg_amount > 600;
-- ============================
-- 📌 7) CTE: Top 2 Highest Spenders
-- ============================
WITH temp_table AS (
SELECT customer_id, SUM(amount) AS total_spending
FROM accounts
GROUP BY customer_id
)
SELECT c.customer_name, t.total_spending
FROM customers c
JOIN temp_table t ON c.customer_id = t.customer_id
ORDER BY t.total_spending DESC
LIMIT 2;
-- ============================
-- 📌 8) CTE: 20% of Total Mode-wise Amount
-- ============================
WITH temp_table AS (
SELECT mode, SUM(amount) AS total_amount
FROM accounts
GROUP BY mode
)
SELECT a.account_id, a.mode, a.amount, t.total_amount
FROM accounts a
JOIN temp_table t ON a.mode = t.mode
WHERE a.amount >= (t.total_amount * 0.20);
-- ============================
-- 📌 9) RECURSIVE CTE – Count from 1 to 5
-- ============================
WITH RECURSIVE cte AS (
SELECT 1 AS num
UNION ALL
SELECT num + 1
FROM cte
WHERE num < 5
)
SELECT num FROM cte;
-- ============================
-- 📌 10) RECURSIVE CTE – Count 1 to 10
-- ============================
WITH RECURSIVE cte AS (
SELECT 1 AS num
UNION ALL
SELECT num + 1
FROM cte
WHERE num < 10
)
SELECT num FROM cte;
-- ============================
-- 📌 11) RECURSIVE CTE – Count Down 10 to 1
-- ============================
WITH RECURSIVE cte AS (
SELECT 10 AS num
UNION ALL
SELECT num - 1
FROM cte
WHERE num > 1
)
SELECT num FROM cte;
-- ============================
-- 📌 12) Multi-CTE Chain (Total → City-wise → Avg City Spend)
-- ============================
WITH total AS (
SELECT customer_id, SUM(amount) AS total_spending
FROM accounts
GROUP BY customer_id
),
city_wise AS (
SELECT c.customer_id, c.customer_name, t.total_spending, c.city
FROM customers c
JOIN total t ON c.customer_id = t.customer_id
),
city_avg AS (
SELECT city, AVG(total_spending) AS avg_city_spending
FROM city_wise
GROUP BY city
)
SELECT cw.customer_name, cw.city, cw.total_spending, ca.avg_city_spending
FROM city_wise cw
JOIN city_avg ca ON cw.city = ca.city
WHERE cw.total_spending > ca.avg_city_spending;
-- ============================================================
-- 🎯 PRACTICE SUMMARY
-- ============================================================
/*
MASTERED TODAY:
✔ Basic CTE (single)
✔ Multi-CTE chains
✔ CTE + JOIN
✔ CTE + Aggregation
✔ CTE + Window Logic
✔ RECURSIVE CTE (increment / decrement)
✔ City-wise & customer-wise analytics
📌 STATUS: SQL CTE MODULE COMPLETED
*/