-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractixeadv.sql
More file actions
269 lines (170 loc) · 4.45 KB
/
practixeadv.sql
File metadata and controls
269 lines (170 loc) · 4.45 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use practiseadvancesql
---SECTION 1: AGGREGATE FUNCTIONS
-- Q1. Find total number of customers.
select count(*) from customers
-- Q2. Find total sales of all orders.
select sum(sales)
from orders
--Q3. Find average order value.
select avg(sales)
from orders
-- Q4. Find highest and lowest product price.
select max(price),
min(price)
from products
-- Q5. Find total sales per customer.
select customerid ,
sum(sales)
from orders
group by customerid
--- Q6. Find total quantity sold per product.
select productid,
sum(quantity)
from orderdetails
group by productid
-- Q7. Find number of orders per customer.
select customerid,
count(*)
from orders
group by customerid
--- SECTION 2: GROUP BY + HAVING
--Q8.Find customers whose total sales > 1000.
select customerid,
sum(sales)
from orders
group by customerid
having sum(sales) > 1000
-- Q9. Find products sold more than 2 times.
select productid,
sum(quantity)
from orderdetails
group by productid
having sum(quantity) > 2
--Q10. Find orders having total quantity > 3.
--- JOINS
--- Q11. Show customer name with their orders.
SELECT c.FirstName, o.OrderID
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID;
Q12. Show order details with product names.
select od.orderid , p
Q13
Show full order report:
(Customer + Product + Quantity + Price)
Q14
Show customers who never placed any orders.
SELECT *
FROM Customers c
LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.CustomerID IS NULL;
--SECTION 4: CASE STATEMENTS
--Q15. Categorize orders:
--High (>500)
--Medium (200–500)
--Low (<200)
select orderid,sales,
CASE
when sales > 500 then 'high'
when sales >= 200 then 'medium'
else 'low'
end as category
from orders
--Q16. Label customers:
--VIP (total sales > 2000)
--Regular (else)
SELECT CustomerID,
CASE
WHEN SUM(Sales) > 2000 THEN 'VIP'
ELSE 'Regular'
END AS Status
FROM Orders
GROUP BY CustomerID;
---Q17. Replace NULL values in LastName with 'Unknown'.
SELECT FirstName,
COALESCE(LastName, 'Unknown')
FROM Customers
🚀 🔥 SECTION 5: SUBQUERIES
Q18
Find products whose price is above average.
select * from products
where price > (select avg(price) from products)
Q19
Find customers who made orders above average sales.
Q20
Find orders made by customers from 'India'.
Q21
Find customers who have NOT placed any orders.
Q22
Find orders where sales > (average sales).
🚀 🔥 SECTION 6: WINDOW FUNCTIONS (VERY IMPORTANT 🔥)
Q23
Show total sales along with each row.
Q24
Show total sales per customer using window function.
Q25
Rank orders by sales (highest to lowest).
Q26
Find top 1 order per customer.
Q27
Show running total of sales.
Q28
Find average sales per product (window).
Q29
Show difference between each order and max sales.
🚀 🔥 SECTION 7: RANKING FUNCTIONS
Q30
Use ROW_NUMBER() to assign unique row numbers to orders.
Q31
Use RANK() to rank customers by total sales.
Q32
Use DENSE_RANK() for product sales ranking.
🚀 🔥 SECTION 8: CTE (COMMON TABLE EXPRESSIONS)
Q33
Use CTE to calculate total sales per customer and display it.
Q34
Use CTE to rank customers based on sales.
Q35
Use recursive CTE to generate numbers from 1 to 20.
Q36
Use CTE to find customers with last order date.
🚀 🔥 SECTION 9: VIEWS
Q37
Create a view showing:
Customer + Order + Sales
Q38
Create a view for top-selling products.
Q39
Create a view for customer total sales summary.
🚀 🔥 SECTION 10: STORED PROCEDURES
Q40
Create procedure to get all customers.
Q41
Create procedure to get orders by CustomerID.
Q42
Create procedure to get total sales by country.
Q43
Create procedure with parameter:
→ get orders above given sales amount
Q44
Create procedure to insert a new order.
🚀 🔥 SECTION 11: TRIGGERS
Q45
Create trigger:
→ After inserting into Orders, log into another table
Q46
Create trigger:
→ Prevent inserting negative sales
Q47
Create trigger:
→ When order is deleted, log it
🚀 🔥 SECTION 12: ADVANCED / MIXED
Q48
Find customer who spent the most money.
Q49
Find most sold product.
Q50
Find revenue per month.
Q51
Find customers who bought more than 2 products in one order.
Q52
Find percentage contribution of each product to total sales.