-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql assignment 6-3.sql
More file actions
431 lines (299 loc) · 14.1 KB
/
sql assignment 6-3.sql
File metadata and controls
431 lines (299 loc) · 14.1 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
-- 1. Write a query to select all data from the `Customers` table.
select * from Customers;
SELECT * FROM assignment.Products;
SELECT * FROM assignment.Inventory;
-- 2. Write a query to select the total number of products from the `Products` table.
select count(*) AS total_products
from products p
;
-- 3. Write a query to select the product name and its price from the `Products` table where the price is greater than 500.
select p.product_name,p.price
from products p
where price > 500;
-- 4. Write a query to find the average price of all products from the `Products` table.
select avg(price)
from products;
-- 5. Write a query to find the total sales amount from the `Sales` table.
select sum(total_amount) as total_sales
from Sales;
-- 6. Write a query to select distinct membership statuses from the `Customers` table.
select distinct membership_status
from customers;
-- 7. Write a query to concatenate first and last names of all customers and show the result as `full_name`.
select concat(first_name,' ',last_name) as full_name
from customers c;
-- 8. Write a query to find all products in the `Products` table where the category is 'Electronics'.
select product_name
from products p
where category = 'Electronics';
-- 9. Write a query to find the highest price from the `Products` table.
select max(price)as highest_price
from Products;
-- 10. Write a query to count the number of sales for each product from the `Sales` table.
SELECT product_id, COUNT(*) AS total_sales
FROM Sales
GROUP BY product_id;
-- 11. Write a query to find the total quantity sold for each product from the `Sales` table.
select product_id ,count(*) as quantity_sold
from sales s
group by product_id;
-- 12. Write a query to find the lowest price of products in the `Products` table.
select min(price) as lowest_price
from products p ;
-- 13. Write a query to find customers who have purchased products with a price greater than 1000.
SELECT DISTINCT c.customer_id, c.first_name, c.last_name
FROM Customers c
JOIN Sales s ON c.customer_id = s.customer_id
JOIN Products p ON s.product_id = p.product_id
WHERE p.price > 1000;
-- 14. Write a query to join the `Sales` and `Products` tables on product_id, and select the product name and total sales amount.
SELECT p.product_name, SUM(s.quantity_sold * p.price) AS total_sales_amount
FROM Sales s
JOIN Products p ON s.product_id = p.product_id
GROUP BY p.product_name;
-- 15. Write a query to join the `Customers` and `Sales` tables and find the total amount spent by each customer.
select c.customer_id,avg(quantity_sold*total_amount) as amount_spent_each
from customers c
join sales s on c.customer_id= s.customer_id
group by c.customer_id;
-- 16. Write a query to join the `Customers`, `Sales`, and `Products` tables, and show each customer's first and last name, product name, and quantity sold.
SELECT c.first_name,
c.last_name,
p.product_name,
s.quantity_sold
FROM Customers c
JOIN Sales s ON c.customer_id = s.customer_id
JOIN Products p ON s.product_id = p.product_id;
-- 17. Write a query to perform a self-join on the `Customers` table and find all pairs of customers who have the same membership status.
SELECT distinct a.first_name AS customer_name,
a.last_name AS customer_name,
b.first_name AS customer2_first_name,
b.last_name AS customer2_last_name,
a.membership_status
FROM Customers a, Customers b
WHERE a.membership_status = b.membership_status
AND a.customer_id < b.customer_id;
-- 18. Write a query to join the `Sales` and `Products` tables, and calculate the total number of sales for each product.
select p.product_name,sum(quantity_sold) as sales_for_each
from products p
join sales s on p.product_id = s.product_id
group by p.product_name;
-- 19. Write a query to find the products in the `Products` table where the stock quantity is less than 10.
select product_name
from products p
where p.stock_quantity <10;
-- 20. Write a query to join the `Sales` table and the `Products` table, and find products with sales greater than 5.
select s.product_id ,p.product_name ,
sum(s.quantity_sold)
from sales s
join products p on s.product_id = p.product_id
GROUP BY s.product_id, p.product_name
having sum (s.quantity_sold) >5;
-- 21. Write a query to select customers who have purchased products that are either in the 'Electronics' or 'Appliances' category.
SELECT DISTINCT c.customer_id, c.first_name, c.last_name
FROM Customers c
JOIN Sales s ON c.customer_id = s.customer_id
JOIN Products p ON s.product_id = p.product_id
WHERE p.category IN ('Electronics', 'Appliances');
-- 22. Write a query to calculate the total sales amount per product and group the result by product name.
select p.product_name,
sum(s.quantity_sold*p.price) as total_sales
from sales s
join products p on s.product_id= p.product_id
group by p.product_name ;
-- 23. Write a query to join the `Sales` table with the `Customers` table and select customers who made a purchase in the year 2023.
select DISTINCT c.customer_id,c.first_name,c.last_name
from customers c
join sales s on c.customer_id = s.customer_id
where EXTRACT( year from s.sale_date )=2023;
-- 24. Write a query to find the customers with the highest total sales in 2023.
select c.customer_id,c.first_name,c.last_name,
SUM(s.quantity_sold * p.price) AS total_sales
from customers c
join sales s on c.customer_id =s.customer_id
join products p on s.product_id = p.product_id
where extract (year from s.sale_date)=2023
group by c.customer_id,c.first_name ,c.last_name
order by total_sales desc
;
-- 25. Write a query to join the `Products` and `Sales` tables and select the most expensive product sold.
select p.product_id,p.product_name,p.price
from products p
join sales s on s.product_id=p.product_id
order by p.price
limit 1;
-- 26. Write a query to find the total number of customers who have purchased products worth more than 500.
select count(c.customer_id) as total_customers
from customers c
join sales s on c.customer_id = s.customer_id
join products p on s.product_id=p.product_id
where p.price >500;
-- 27. Write a query to join the `Products`, `Sales`, and `Customers` tables and find the total number of sales made by customers who are in the 'Gold' membership tier.
select count(*) as total_sales
from products p
join sales s on p.product_id = s.product_id
join Customers c on s.customer_id= c.customer_id
where c.membership_status = 'Gold';
-- 28. Write a query to join the `Products` and `Inventory` tables and find all products that have low stock (less than 10).
select min(i.stock_quantity) as low_stock
from products p
join inventory i on p.product_id = i.product_id
where i.stock_quantity < 10;
-- 29. Write a query to find customers who have purchased more than 5 products and show the total quantity of products they have bought.
SELECT c.customer_id,c.first_name,c.last_name,
SUM(s.quantity_sold) AS total_quantity
FROM Customers c
JOIN Sales s ON c.customer_id = s.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
HAVING SUM(s.quantity_sold) > 5;
-- 30. Write a query to find the average quantity sold per product.
SELECT product_id,
AVG(quantity_sold) AS average_quantity_sold
FROM Sales s
GROUP BY product_id;
-- 31. Write a query to find the number of sales made in the month of December 2023.
select count(*) as total_sales
from sales s
WHERE EXTRACT(YEAR FROM s.sale_date) = 2023
AND EXTRACT(MONTH FROM s.sale_date) = 12;
-- 32. Write a query to find the total amount spent by each customer in 2023 and list the customers in descending order.
SELECT c.customer_id,c.first_name,c.last_name,
SUM(s.quantity_sold * p.price) AS total_amount
FROM Customers c
JOIN Sales s ON c.customer_id = s.customer_id
JOIN Products p ON p.product_id = s.product_id
WHERE EXTRACT(YEAR FROM s.sale_date) = 2023
GROUP BY c.customer_id, c.first_name, c.last_name
ORDER BY total_amount DESC;
-- 33. Write a query to find all products that have been sold but have less than 5 units left in stock.
select distinct p.product_id,p.product_name,p.stock_quantity
from products p
join Sales s on s.product_id =p.product_id
GROUP BY p.product_id, p.product_name
having sum (s.quantity_sold) <5;
-- 34. Write a query to find the total sales for each product and order the result by the highest sales.
SELECT p.product_id, p.product_name,
SUM(s.quantity_sold ) AS total_sales
FROM Products p
JOIN Sales s ON p.product_id = s.product_id
GROUP BY p.product_id, p.product_name
ORDER BY total_sales DESC;
-- 35. Write a query to find all customers who bought products within 7 days of their registration date.
SELECT DISTINCT c.customer_id,c.first_name,c.last_name,c.registration_date,s.sale_date
FROM Customers c
JOIN Sales s ON c.customer_id = s.customer_id
WHERE s.sale_date <= c.registration_date + INTERVAL '7 days';
-- 36. Write a query to join the `Sales` table with the `Products` table and filter the results by products priced between 100 and 500.
select s.product_id, s.sale_id,s.customer_id,p.product_name, p.price,s.quantity_sold
from sales s
join products p on s.product_id = p.product_id
WHERE p.price BETWEEN 100 AND 500;
-- 37. Write a query to find the most frequent customer who made purchases from the `Sales` table.
select c.customer_id,c.first_name,c.last_name,
count(s.sale_id) as total_purchases
from customers c
join sales s on c.customer_id = s.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
ORDER BY total_purchases desc
LIMIT 1;
-- 38. Write a query to find the total quantity of products sold per customer.
select c.customer_id,c.first_name,c.last_name,
count(s.quantity_sold) as total_sold
from customers c
join sales s on c.customer_id =s.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
order by total_sold desc;
-- 39. Write a query to find the products with the highest stock and lowest stock, and display them together in a single result set.
(SELECT product_id, product_name, stock_quantity, 'Highest Stock' AS stock_type
FROM Products
ORDER BY stock_quantity DESC
LIMIT 1)
UNION
(SELECT product_id, product_name, stock_quantity, 'Lowest Stock' AS stock_type
FROM Products
ORDER BY stock_quantity ASC
LIMIT 1);
-- 40. Write a query to find products whose names contain the word 'Phone' and their total sales.
SELECT p.product_id,p.product_name,
SUM(s.quantity_sold) AS total_sales
FROM Products p
JOIN Sales s ON p.product_id = s.product_id
WHERE p.product_name ILIKE '%Phone%'
GROUP BY p.product_id, p.product_name
ORDER BY total_sales DESC;
-- 41. Write a query to perform an `INNER JOIN` between `Customers` and `Sales`, then display the total sales amount and the product names for customers in the 'Gold' membership status.
select c.customer_id,c.first_name,c.last_name,p.product_name,
sum(s.quantity_sold*p.price) as Total_sales
from Customers c
inner join sales s on c.customer_id=s.customer_id
inner join Products p on s.product_id=p.product_id
where c.membership_status ='Gold'
GROUP BY c.customer_id, c.first_name, c.last_name, p.product_name
ORDER BY Total_sales DESC;
-- 42. Write a query to find the total sales of products by category.
select p.category,
sum(s.quantity_sold * p.price) as total_sales
from Products p
join sales S on s.product_id =p.product_id
GROUP BY p.category
ORDER BY total_sales DESC;
-- 43. Write a query to join the `Products` table with the `Sales` table, and calculate the total sales for each product, grouped by month and year.
SELECT p.product_name,
EXTRACT(YEAR FROM s.sale_date) AS year,
EXTRACT(MONTH FROM s.sale_date) AS month,
SUM(s.quantity_sold * p.price) AS total_sales
FROM Products p
JOIN Sales s ON p.product_id = s.product_id
GROUP BY p.product_name,
EXTRACT(YEAR FROM s.sale_date),
EXTRACT(MONTH FROM s.sale_date)
ORDER BY year, month;
-- 44. Write a query to join the `Sales` and `Inventory` tables and find products that have been sold but still have stock remaining.
SELECT s.product_id
FROM Sales s
JOIN Inventory i ON s.product_id = i.product_id
WHERE i.stock_quantity > 0;
-- 45. Write a query to find the top 5 customers who have made the highest purchases.
select c.customer_id,c.first_name,c.last_name,
SUM(s.quantity_sold * p.price) AS total_purchase
from customers c
join sales s on s.customer_id =c.customer_id
JOIN Products p ON s.product_id = p.product_id
GROUP BY c.customer_id, c.first_name, c.last_name
ORDER BY total_purchase DESC
LIMIT 5;
-- 46. Write a query to calculate the total number of unique products sold in 2023.
SELECT COUNT(DISTINCT product_id)
FROM Sales s
WHERE EXTRACT(YEAR FROM sale_date) = 2023;
-- 47. Write a query to find the products that have not been sold in the last 6 months.
select p.product_id,p.product_name
from products p
left join sales s on p.product_id = s.product_id
And s.sale_date >= CURRENT_DATE - INTERVAL '6 months'
Where s.product_id IS NULL;
-- 48. Write a query to select the products with a price range between $200 and $800, and find the total quantity sold for each.
SELECT p.product_id, p.product_name,
SUM(s.quantity_sold) AS total_sold
FROM products p
JOIN sales s ON p.product_id = s.product_id
WHERE p.price BETWEEN 200 AND 800
GROUP BY p.product_id, p.product_name;
-- 49. Write a query to find the customers who spent the most money in the year 2023.
select C.customer_id,c.first_name,c.last_name,
sum(s.quantity_sold*p.price) as most_spent
from customers c
join sales s on c.customer_id = s.customer_id
join products p on p.product_id =s.product_id
where extract (year from sale_date)=2023
group by c.customer_id ,c.first_name ,c.last_name
order by most_spent desc
limit 1;
-- 50. Write a query to select the products that have been sold more than 100 times and have a price greater than 200.
SELECT p.product_id, p.product_name,
SUM(s.quantity_sold) AS total_sold
FROM Products p
JOIN Sales s ON p.product_id = s.product_id
WHERE p.price > 200
GROUP BY p.product_id, p.product_name
HAVING SUM(s.quantity_sold) > 100;