-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_queries.sql
More file actions
423 lines (307 loc) · 11.2 KB
/
Copy path03_queries.sql
File metadata and controls
423 lines (307 loc) · 11.2 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
-- Basic SELECT
-- Q1 Show all active customers.
select * from customers
where Status = 'active';
-- Q2 Display restaurant names, city and average rating.
select RestaurantName, city, RATING
from Restaurants;
-- Q3 List all menu items costing more than ₹300.
select ItemName, Price
from menuitems
where price > 300;
-- Q4 Find all restaurants that are currently open.
select RestaurantName,Status
from Restaurants
where Status = 'Open';
-- Q5 Display customers who signed up after January 1, 2023.
select CustomerID, FirstName, SignupDate
from customers
where SignupDate > '2023-01-01' ;
-- WHERE + ORDER BY + LIMIT
-- Q6 Show the 10 most expensive menu items.
select ItemName, price
from menuitems
order by price desc
limit 10;
-- Q7 Find all orders whose total amount is greater than ₹1000.
select OrderID, TotalAmount
from orders
where TotalAmount > 1000;
-- Q8 Display restaurants with ratings above 4.5 sorted by rating.
select RestaurantName, Rating
from restaurants
where rating > 4.5
order by Rating asc;
-- Q9 Find customers from Jaipur.
select FirstName, city
from customers
where city = 'jaipur';
-- Q10 Display the latest 10 orders.
select OrderID, OrderDate
from orders
order by OrderDate desc
limit 10;
-- DISTINCT
-- Q11 Find all unique cities where customers live.
select distinct (City)
from customers;
-- Q12 List different restaurant categories.
select distinct (CategoryName)
from RestaurantCategories;
-- Q13 Find all distinct payment methods.
select distinct PaymentMethod
from Payments;
-- Q14 Display unique order statuses.
select distinct OrderStatus
from orders;
-- Q15 Find all cities where restaurants operate.
select distinct City
from restaurants;
-- ========================================================================================
-- Aggregate Functions
-- Q16 Total revenue generated.
select sum(TotalAmount) as Revenue
from orders;
-- Q17 Average menu price.
select avg(Price)
from MenuItems;
-- Q18 Highest order amount.
select max(TotalAmount)
from orders;
-- Q19 Lowest order amount.
select min(TotalAmount)
from orders;
-- Q20 Number of registered customers.
select count(customerid)
from customers;
-- ========================================================================================
-- GROUP BY
-- Q21 Number of restaurants in each category.
select CategoryID, count(RestaurantID)
from restaurants
group by (CategoryID);
-- Q22 Total orders placed by each customer.
select CustomerID, count(OrderID)
from orders
group by (CustomerID);
-- Q23 Average menu price for each restaurant.
Select RestaurantID, avg(Price) as AvgPrice
from MenuItems
group by RestaurantID;
-- Q24 Revenue generated by each restaurant.
select restaurantid, sum(TotalAmount) as Reveneue
from orders
group by RestaurantID;
-- Q25 Number of reviews each restaurant received
select RestaurantID, count(ReviewID)
from reviews
group by RestaurantID;
-- ========================================================================================
-- HAVING CLAUSE
-- Q26 Restaurants with more than 10 menu items.
select RestaurantID, count(MenuItemID)
from menuitems
group by RestaurantID
having count(MenuItemID) > 10;
-- Q27 Customers who placed more than 5 orders.
select customerID, count(orderid) as TotalOrders
from orders
group by CustomerID
having TotalOrders > 5;
-- Q28 Restaurants earning more than ₹10,000.
select RestaurantID, sum(TotalAmount)
from orders
group by RestaurantID
having sum(TotalAmount) > 10000;
-- Q29 Categories having more than 3 restaurants.
select CategoryID ,count(RestaurantID) as TotalRestaurants
from restaurants
group by CategoryID
having count(restaurantID) > 3;
-- Q30 Restaurants with average rating greater than 4.5.
select RestaurantID, avg(Rating) as AvgRating
from reviews
group by RestaurantID
having avg(rating) > 4.5
order by avgrating asc;
-- ===========================================================================================================
-- INNER JOIN
-- Q31 Display customer name with their orders.
select FirstName, TotalAmount, OrderStatus, OrderDate
from customers
inner join orders
on customers.CustomerID = orders.CustomerID;
-- Q32 Show restaurant names with their categories.
select restaurants.RestaurantName, RestaurantCategories.CategoryName
from restaurants
inner join restaurantcategories
on restaurants.CategoryID = restaurantcategories.CategoryID;
-- Q33 Display menu item along with restaurant name.
select menuitems.ItemName, restaurants.RestaurantName
from menuitems
inner join restaurants
on menuitems.RestaurantID = restaurants.RestaurantID;
-- Q34 Show payment details with customer name.
select customers.FirstName, orders.TotalAmount, payments.amount, payments.PaymentStatus
from customers
inner join orders
on customers.CustomerID = orders.CustomerID
inner join payments
on orders.OrderID = payments.OrderID;
-- Q35 Display review with restaurant and customer names.
select customers.FirstName, restaurants.RestaurantName, reviews.Rating, reviews.Comment
from customers
inner join reviews
on customers.CustomerID = reviews.CustomerID
inner join restaurants
on restaurants.RestaurantID = reviews.RestaurantID;
-- Q Which customer ordered from which restaurant and what he ordered?
SELECT Customers.FirstName, Restaurants.RestaurantName, MenuItems.ItemName
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID = Customers.CustomerID
INNER JOIN Restaurants
ON Orders.RestaurantID = Restaurants.RestaurantID
INNER JOIN OrderItems
ON Orders.OrderID = OrderItems.OrderID
INNER JOIN MenuItems
ON OrderItems.MenuItemID = MenuItems.MenuItemID;
-- ===========================================================================================================
-- LEFT JOIN
-- Q36 Find customers who never placed an order.
select customers.CustomerID, customers.FirstName, orders.OrderID
from customers
left join orders
on customers.CustomerID = orders.CustomerID
WHERE Orders.OrderID IS NULL;
-- Q37 Show all restaurants including those with no reviews.
select restaurants.RestaurantID, restaurants.RestaurantName, reviews.Comment, reviews.ReviewID
from restaurants
left join reviews
on restaurants.RestaurantID = reviews.RestaurantID;
-- Q37.A) Find restaurants that have no reviews.
select restaurants.RestaurantID, restaurants.RestaurantName, reviews.Comment, reviews.ReviewID
from restaurants
left join reviews
on restaurants.RestaurantID = reviews.RestaurantID
where reviews.RestaurantID is null;
-- Q38 Show all restaurants even if they have no menu items.
select restaurants.RestaurantName, menuitems.ItemName
from restaurants
left join menuitems
on restaurants.RestaurantID = menuitems.RestaurantID;
-- Q38.A) Find restaurants that have no menu items.
select restaurants.RestaurantName, menuitems.ItemName
from restaurants
left join menuitems
on restaurants.RestaurantID = menuitems.RestaurantID
WHERE MenuItems.MenuItemID IS null;
-- Q39 List delivery partners including those with no deliveries.
select deliverypartners.PartnerID, deliverypartners.PartnerName, orders.PartnerID
from deliverypartners
left join orders
on deliverypartners.PartnerID = orders.PartnerID;
-- ===========================================================================================================
-- RIGHT JOIN
-- Q41 Show all menu items with restaurants.
SELECT menuitems.ItemName, restaurants.RestaurantName
from restaurants
right join menuitems
on menuitems.RestaurantID = restaurants.RestaurantID;
-- Q42 Display every payment even if order details are missing.
select payments.amount, payments.PaymentStatus, orders.OrderStatus
from orders
right join payments
on payments.OrderID = orders.OrderID;
-- Q43 Show every review with restaurant details.
select restaurants.RestaurantName, reviews.Comment
from restaurants
right join reviews
on restaurants.RestaurantID = reviews.RestaurantID;
-- Q44 List every order item with menu information.
select menuitems.ItemName, orderitems.Price, orderitems.Quantity
from menuitems
right outer join orderitems
on menuitems.MenuItemID = orderitems.MenuItemID;
-- Q45 Display all categories even if no restaurants belong to them.
select restaurantcategories.CategoryName, restaurants.RestaurantName
from restaurants
right join restaurantcategories
on restaurantcategories.CategoryID = restaurants.CategoryID;
-- ===========================================================================================================
-- MULTIPLE JOIN
-- Q46 Customer → Order → Payment details.
select customers.FirstName, orders.OrderStatus, payments.amount
from customers
inner join orders
on customers.CustomerID = orders.CustomerID
inner join payments
on orders.OrderID = payments.OrderID;
-- Q47 Restaurant → Menu → OrderItems.
select restaurants.RestaurantName, menuitems.ItemName, orderitems.Price
from restaurants
inner join menuitems
on restaurants.RestaurantID = menuitems.RestaurantID
inner join orderitems
on orderitems.MenuItemID = menuitems.MenuItemID;
-- Q48 Customer → Review → Restaurant.
select customers.FirstName, restaurants.RestaurantName, reviews.Comment
from customers
inner join reviews
on customers.CustomerID = reviews.CustomerID
inner join restaurants
on restaurants.RestaurantID = reviews.RestaurantID;
-- Q49 Order → Delivery Partner → Payment.
select orders.OrderID, deliverypartners.PartnerName, payments.PaymentStatus
from orders
inner join payments
on orders.OrderID = payments.OrderID
inner join deliverypartners
on deliverypartners.PartnerID = orders.PartnerID;
-- Q50 Restaurant → Category → Reviews.
select restaurants.RestaurantName, restaurantcategories.CategoryName, reviews.Rating, reviews.Comment
from restaurants
inner join restaurantcategories
on restaurants.CategoryID = restaurantcategories.CategoryID
inner join reviews
on reviews.RestaurantID = restaurants.RestaurantID;
-- ===========================================================================================================
-- Subqueries
-- TODO: pending
-- Q51 Find customers who spent more than the average customer.
-- Q52 Find restaurants with rating above average.
select RestaurantName
from restaurants
where Rating > (select avg(Rating) from restaurants);
-- Q53 Display menu items priced above average.
select ItemName
from menuitems
where Price > (select avg(price) from menuitems);
-- Q54 Find customers who placed the highest value order.
select FirstName, TotalAmount
from customers
inner join orders
on customers.CustomerID = orders.CustomerID
where orders.TotalAmount = (select max(TotalAmount) from orders);
-- TODO: pending
-- Q55 Find restaurants earning more than average revenue.
-- ===========================================================================================================
-- Correlated Subqueries
-- Q56 Find restaurants whose average menu price is higher than overall average.
select RestaurantName, avg(Price)
from restaurants
inner join menuitems
on restaurants.RestaurantID = menuitems.RestaurantID
group by restaurants.RestaurantID
having avg(price) >
(
select avg(price) from menuitems as Restaurant_AVG
);
-- TODO: pending
-- Q57 Find customers who spent more than every customer in their city.
-- TODO: pending
-- Q58 Find restaurants whose ratings exceed category average.
-- TODO: pending
-- Q59 Find orders containing the highest priced item.
-- TODO: pending
-- Q60 Find restaurants whose menu has more items than average.