Repository navigation
Expand file tree
/
Copy pathSQL-Project-PostgreSQL.sql
More file actions
1694 lines (1501 loc) · 46.2 KB
/
Copy pathSQL-Project-PostgreSQL.sql
File metadata and controls
1694 lines (1501 loc) · 46.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
CREATE TABLE customers (
customer_id INT PRIMARY KEY ,
customer_name VARCHAR(100),
gender VARCHAR(10),
age INT CHECK(age >=18) ,
city VARCHAR(50),
state VARCHAR (50),
signup_date DATE);
CREATE TABLE products(
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
category VARCHAR(50),
sub_category VARCHAR(50),
brand VARCHAR(50),
unit_price DECIMAL(10,2) CHECK(unit_price > 0) );
CREATE TABLE orders(
order_id INT PRIMARY KEY,
customer_id INT REFERENCES customers(customer_id),
order_date DATE,
payment_method VARCHAR (30),
order_status VARCHAR (30)
CHECK(order_status IN('Pending','Completed','Cancelled','Returned')) ,
shipping_city VARCHAR (50) );
CREATE TABLE orders_item(
order_item_id INT PRIMARY KEY,
order_id INT REFERENCES orders(order_id),
product_id INT REFERENCES products(product_id),
quantity INT CHECK(quantity > 0),
discount DECIMAL(10,2)DEFAULT 0
CHECK(discount >= 0) );
SELECT * FROM customers;
SELECT * FROM products;
SELECT * FROM orders;
SELECT * FROM orders_item;
-- Database Relationship
-- Customers --> Orders --> Orders_Item <-- Products
/*
====================================================
SQL E-COMMERCE PROJECT
====================================================
Project: E-Commerce Sales Analysis
Database: PostgreSQL
Author: Punit Godiyal
Purpose: SQL Practice for Data Analyst Portfolio
Topic: SQL Queries
====================================================
*/
-- Question 1:
-- Display all customer details.
SELECT * FROM customers;
-- Question 2:
-- Display only the customer names and their cities.
SELECT customer_name , city
FROM customers;
-- Question 3:
--Display all customers who are older than 30 years.
SELECT customer_name , age
FROM customers
WHERE age>30;
-- Question 4:
--Display all female customers.
SELECT customer_name , gender
FROM customers
WHERE gender='Female';
-- Question 5:
-- Display all customers who live in Delhi
SELECT customer_name , city
FROM customers
WHERE city='Delhi';
-- Question 6:
-- Display all products whose unit price is greater than ₹2,000.
SELECT product_name , unit_price
FROM products
WHERE unit_price>2000;
-- Question 7:
-- Display all products that belong to the Electronics category.
SELECT product_name , category
FROM products
WHERE Category='Electronics';
--Question 8:
-- Display all orders whose status is Completed.
SELECT order_id , order_status
FROM orders
WHERE order_status='Completed';
--Question 9:
--Display all customers who signed up after 1 January 2024
SELECT customer_name , signup_date
FROM customers
WHERE signup_date>'2024-01-01';
--Question 10:
--Display the first 10 products, ordered by unit price from highest to lowest.
SELECT product_name , unit_price
FROM products
ORDER BY unit_price DESC
LIMIT 10;
--Question 11:
--Display all unique cities where customers live.
SELECT DISTINCT city
FROM customers;
--Question 12:
--Display all products sorted by unit price from lowest to highest.
SELECT product_name , unit_price
FROM products
ORDER BY unit_price ASC;
--Question 13:
--Display all customers whose names start with the letter 'A'.
SELECT customer_name
FROM customers
WHERE customer_name
LIKE 'A%';
--Question 14:
--Display all customers whose names end with the letter 'n'.
SELECT customer_name
FROM customers
WHERE customer_name
LIKE '%n';
--Question 15:
--Display all customers whose age is between 25 and 35 years (inclusive).
SELECT customer_name , age
FROM customers
WHERE age
BETWEEN 25 AND 35;
--Question 16:
--Display all customers who live in Delhi, Mumbai, or Pune.
SELECT customer_name , city
FROM customers
WHERE city
IN ( 'Delhi','Mumbai','Pune');
--Question 17:
--Display all female customers who are older than 30 years.
SELECT customer_name , age , gender
FROM customers
WHERE gender='Female' AND age>30;
--Question 18:
--Display all products that belong to the Electronics category OR have a unit price greater than ₹5,000.
SELECT product_name , category , unit_price
FROM products
WHERE category='Electronics' OR unit_price>5000;
--Question 19:
--Display all completed orders placed after 1 March 2024
SELECT order_id, order_date, order_status
FROM orders
WHERE order_date > '2024-03-01'
AND order_status = 'Completed';
--Question 20:
--Display the top 5 most expensive products in the Electronics category.
SELECT product_name , unit_price , category
FROM products
WHERE category='Electronics'
ORDER BY unit_price DESC
LIMIT 5 ;
--Question 21:
--Display all customers except those who live in Delhi.
SELECT customer_name , city
FROM customers
WHERE NOT city='Delhi';
--Question 22:
--Display all products except those in the Electronics category.
SELECT product_name , category
FROM products
WHERE NOT category='Electronics';
--Question 23:
--Display all orders whose status is not 'Cancelled'.
SELECT order_id , order_status
FROM orders
WHERE order_status<>'Cancelled';
--Question 24:
--Display all customers whose age is not between 25 and 35.
SELECT customer_name , age
FROM customers
WHERE NOT age BETWEEN 25 AND 35;
--Question 25:
--Display all products whose unit price is between ₹1,000 and ₹5,000, sorted from lowest to highest price.
SELECT product_name , unit_price
FROM products
WHERE unit_price
BETWEEN 1000 AND 5000
ORDER BY unit_price;
--Question 26:
--Display the 5 cheapest products.
SELECT product_name , unit_price
FROM products
ORDER BY unit_price
LIMIT 5;
--Question 27:
--Display all customers whose names contain the letter 'a'.
SELECT customer_name , age
FROM customers
WHERE customer_name
LIKE '%a%';
--Question 28:
--Display all customers whose names have exactly five characters.
SELECT customer_name , age
FROM customers
WHERE customer_name
LIKE '_____';
--Question 29:
--Display all orders placed in the year 2024, sorted by the most recent order first.
SELECT order_id , order_date
FROM orders
WHERE order_date
BETWEEN '2024-01-01' AND '2024-12-31'
ORDER BY order_date DESC;
--Question 30:
--Display all products whose category is either Electronics or Furniture ,and whose unit price is greater than ₹2,000.
SELECT product_name, category, unit_price
FROM products
WHERE category IN ('Electronics', 'Furniture')
AND unit_price > 2000;
--Question 31:
--Count the total number of customers.
SELECT COUNT(customer_name) AS total_customers
FROM customers;
--Question 32:
--Count the total number of products.
SELECT COUNT(product_name) AS total_products
FROM products;
--Question 33:
--Find the average age of all customers.
SELECT AVG(age)
FROM customers;
--Question 34:
--Find the minimum and maximum unit price of products.
SELECT MAX(unit_price) AS highest_price ,
MIN(unit_price) AS lowest_price
FROM products;
--Question 35:
--Find the total value of all product prices.
SELECT SUM(unit_price)
FROM products;
--Question 36:
--Find the average unit price of products in the Electronics category.
SELECT AVG (unit_price)
FROM products
WHERE category='Electronics';
--Question 37:
--Count how many orders have the status 'Completed'.
SELECT COUNT(order_id)
FROM orders
WHERE order_status='Completed';
--Question 38:
--Find the highest-priced product.
SELECT product_name , unit_price
FROM products
ORDER BY unit_price DESC
LIMIT 1;
--Question 39:
--Find the lowest-priced product.
SELECT product_name , unit_price
FROM products
ORDER BY unit_price
LIMIT 1;
--Question 40:
--Find the total number of customers from Delhi.
SELECT COUNT(customer_name)
FROM customers
WHERE city = 'Delhi';
--Question 41:
--Count the number of customers in each city.
SELECT city , COUNT (customer_name)
FROM customers
GROUP BY city;
--Question 42:
--Find the average age of customers in each city.
SELECT city , AVG(age)
FROM customers
GROUP BY city;
--Question 43:
--Count the number of products in each category.
SELECT category , COUNT(product_name)
FROM products
GROUP BY category;
--Question 44:
--Find the average unit price for each product category.
SELECT category , AVG(unit_price)
FROM products
GROUP BY category;
--Question 45:
--Find the highest unit price in each product category.
SELECT category , MAX(unit_price)
FROM products
GROUP BY category;
--Question 46:
--Count the number of orders for each order status.
SELECT order_status , COUNT(order_id)
FROM orders
GROUP BY order_status;
--Question 47:
--Find the total value of products in each category.
SELECT category , SUM(unit_price)
AS total_category_value
FROM products
GROUP BY category;
--Question 48:
--Find the minimum and maximum unit price for each category.
SELECT category , MAX(unit_price) , MIN(unit_price)
FROM products
GROUP BY category;
--Question 49:
--Count the number of male and female customers.
SELECT gender, COUNT(customer_name)
AS total_customers
FROM customers
GROUP BY gender;
--Question 50:
--Find the average age of customers based on gender.
SELECT gender,
AVG(age) AS average_age
FROM customers
GROUP BY gender;
--Question 51:
--Find the cities that have more than 5 customers.
SELECT city,
COUNT(*) AS total_customers
FROM customers
GROUP BY city
HAVING COUNT(*) > 5;
--Question 52:
--Find the product categories that have more than 10 products.
SELECT category ,
COUNT(*) AS total_products
FROM products
GROUP BY category
HAVING COUNT(*) > 10;
--Question 53:
--Find the order statuses that have more than 20 orders.
SELECT order_status ,
COUNT(*) AS total_orders
FROM orders
GROUP BY order_status
HAVING COUNT(*) > 20;
--Question 54:
--Find the cities where the average customer age is greater than 30.
SELECT city ,
AVG(age) AS average_age
FROM customers
GROUP BY city
HAVING AVG(age) > 30;
--Question 55:
--Find the product categories where the average unit price is greater than 5000.
SELECT category ,
AVG(unit_price) AS average_price
FROM products
GROUP BY category
HAVING AVG(unit_price) >5000;
--Question 56:
--Find the product categories where the highest unit price is greater than 10000.
SELECT category ,
MAX(unit_price) AS maximum_price
FROM products
GROUP BY category
HAVING MAX(unit_price)>10000;
--Question 57:
--Find the product categories where the total value of products is greater than 10000.
SELECT category ,
SUM(unit_price) AS total_price
FROM products
GROUP BY category
HAVING SUM(unit_price) > 10000;
--Question 58:
--Find the genders that have more than 10 customers.
SELECT gender,
COUNT(*) AS total_customers
FROM customers
GROUP BY gender
HAVING COUNT(*) > 10;
--Question 59:
--Find the product categories where the minimum unit price is less than 1000.
SELECT category,
MIN(unit_price) AS minimum_price
FROM products
GROUP BY category
HAVING MIN(unit_price) < 1000;
--Question 60:
--Find the cities where the total number of customers is between 5 and 15.
SELECT city ,
COUNT(*) AS total_customerS
FROM customers
GROUP BY city
HAVING COUNT(*) BETWEEN 5 AND 15;
--Question 61:
--Display the order_id, customer_name, and order_date for all orders.
SELECT customer_name, order_id, order_date
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;
--Question 62:
--Display the customer_name, city, and order_status for all orders.
SELECT customer_name , city , order_status
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;
--Question 63:
--Display the order_id, customer_name, product_name, and quantity for all orders.
SELECT o.order_id , c.customer_name , p.product_name ,oi.quantity
FROM orders AS o
INNER JOIN customers AS c
ON o.customer_id = c.customer_id
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON oi.product_id = p.product_id;
--Question 64:
--Display the product_name, category, quantity, and total_amount for each order.
SELECT p.product_name, p.category , oi.quantity ,
(oi.quantity * p.unit_price) AS total_amount
FROM products AS p
INNER JOIN orders_item AS oi
ON p.product_id = oi.product_id;
--Question 65:
--Display the customer_name, product_name, order_date, and total_amount for completed orders only.
SELECT c.customer_name, p.product_name, o.order_date,
(oi.quantity * p.unit_price) - oi.discount AS total_amount
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON oi.product_id = p.product_id
WHERE o.order_status = 'Completed';
--Question 66:
--Display the customer_name and total number of orders placed by each customer.
SELECT c.customer_name,
COUNT(o.order_id) AS total_orders
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
GROUP BY c.customer_name;
--Question 67:
--Display the product_name and total quantity sold for each product.
SELECT p.product_name,
SUM(oi.quantity) AS total_quantity_sold
FROM products AS p
INNER JOIN orders_item AS oi
ON p.product_id = oi.product_id
GROUP BY p.product_name;
--Question 68:
--Display the customer_name and total amount spent by each customer.
SELECT c.customer_name , SUM(oi.quantity * p.unit_price) AS total_amount_spent
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON p.product_id = oi.product_id
GROUP BY c.customer_name;
--Question 69:
--Display the product_name and the number of times each product was ordered.
SELECT p.product_name , COUNT(oi.product_id)
AS total_orders
FROM products AS p
INNER JOIN orders_item AS oi
ON p.product_id = oi.product_id
GROUP BY product_name;
--Question 70:
--Display the customer_name, city, total number of orders, and total amount spent by each customer.
SELECT c.customer_name, c.city,
COUNT(DISTINCT o.order_id) AS total_orders,
SUM((oi.quantity * p.unit_price) - oi.discount) AS total_amount_spent
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON oi.product_id = p.product_id
GROUP BY c.customer_name, c.city;
--Question 71
--Display the customer_name and the total number of orders placed by each customer.
SELECT c.customer_name , COUNT(o.order_id)
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
GROUP BY c.customer_name ;
--Question 72
---Display the city and the total amount spent by customers from each city.
SELECT c.city , SUM(oi.quantity * p.unit_price)
AS total_amount
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item as oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON p.product_id = oi.product_id
GROUP BY c.city ;
--Question 73
--Display the product_name and the total quantity sold for each product.
SELECT p.product_name,
SUM(oi.quantity) AS total_quantity_sold
FROM products AS p
INNER JOIN orders_item AS oi
ON p.product_id = oi.product_id
GROUP BY p.product_name;
--Question 74
--Display the customer_name and the number of different products purchased by each customer.
SELECT c.customer_name , COUNT(DISTINCT p.product_id)
AS total_products
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON oi.product_id = p.product_id
GROUP BY c.customer_name ;
--Question 75
--Display the category and the average quantity ordered for products in each category.
SELECT p.category , AVG(oi.quantity)
AS average_quantity
FROM products AS p
INNER JOIN orders_item AS oi
ON p.product_id = oi.product_id
GROUP BY p.category;
--Question 76
--Display the customer_name and the date of their latest order.
SELECT c.customer_name , MAX(o.order_date)
AS latest_order
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
GROUP BY c.customer_name;
--Question 77
--Display the product_name and the total revenue generated by each product.
SELECT p.product_name , SUM(oi.quantity * p.unit_price - oi.discount * oi.quantity)
AS total_revenue
FROM products AS p
INNER JOIN orders_item AS oi
ON p.product_id = oi.product_id
GROUP BY p.product_name ;
--Question 78
--Display the city and the number of customers who have placed at least one order.
SELECT c.city,COUNT(DISTINCT c.customer_id)
AS total_customers
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
GROUP BY c.city;
--Question 79
--Display the order_status and the total revenue generated from each order status.
SELECT o.order_status , SUM(p.unit_price*oi.quantity - oi.discount)
AS total_revenue
FROM orders AS o
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON p.product_id = oi.product_id
GROUP BY o.order_status;
--Question 80
--Display the top 3 customers who have spent the highest total amount.
SELECT c.customer_name , SUM(p.unit_price*oi.quantity)
AS total_amount
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item as oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON p.product_id = oi.product_id
GROUP BY c.customer_name
ORDER BY total_amount DESC
LIMIT 3;
-- Question 81
--Display all customers along with their order_id. Include customers who have never placed an order.
SELECT c.customer_name , o.order_id
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id;
-- Question 82
--Display all customers and the total number of orders placed by each customer. Customers with no orders should also appear.
SELECT c.customer_name , COUNT(o.order_id)
AS total_orders
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
GROUP BY c.customer_name ;
-- Question 83
--Display all products and the quantity sold. Include products that have never been sold.
SELECT p.product_name,
COALESCE(SUM(oi.quantity), 0) AS total_quantity_sold
FROM products AS p
LEFT JOIN orders_item AS oi
ON p.product_id = oi.product_id
GROUP BY p.product_name;
-- Question 84
--Display all products along with the total revenue generated by each product. Products with no sales should also appear.
SELECT p.product_name,
COALESCE(SUM((oi.quantity * p.unit_price) - oi.discount), 0) AS total_revenue
FROM products AS p
LEFT JOIN orders_item AS oi
ON p.product_id = oi.product_id
GROUP BY p.product_name;
-- Question 85
--Display all customers along with their latest order date. Customers who have never placed an order should also appear.
SELECT c.customer_name , MAX(o.order_date)
AS latest_date
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
GROUP BY c.customer_name ;
-- Question 86
--Display all cities and the total number of orders placed by customers from each city. Cities with customers who never ordered should also appear.
SELECT c.city , COUNT(o.order_id)
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
GROUP BY c.city ;
-- Question 87
--Display all products along with the number of customers who purchased each product. Include products that have never been purchased.
SELECT p.product_name , COUNT(DISTINCT c.customer_id)
FROM products AS p
LEFT JOIN orders_item AS oi
ON p.product_id = oi.product_id
LEFT JOIN orders AS o
ON oi.order_id = o.order_id
LEFT JOIN customers AS c
ON o.customer_id = c.customer_id
GROUP BY p.product_name ;
-- Question 88
--Display all customers along with the total amount spent by each customer. Customers who never placed an order should show a total amount of 0.
SELECT c.customer_name , COALESCE(SUM(p.unit_price * oi.quantity),0)
AS total_amount
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
LEFT JOIN orders_item as oi
ON o.order_id = oi.order_id
LEFT JOIN products AS p
ON p.product_id = oi.product_id
GROUP BY c.customer_name ;
-- Question 89
--Display all products along with their category and the total quantity sold. Products with no sales should display 0.
SELECT p.product_name , p.category , COALESCE(SUM(oi.quantity), 0)
AS total_quantity
FROM products AS p
LEFT JOIN orders_item AS oi
ON p.product_id = oi.product_id
GROUP BY p.product_name , p.category ;
-- Question 90
--Display the top 5 customers who spent the highest amount. Include customers with no orders (their total amount should be 0).
SELECT c.customer_name , COALESCE(SUM(p.unit_price * oi.quantity),0)
AS total_amount
FROM customers AS c
LEFT JOIN orders AS o
ON c.customer_id = o.customer_id
LEFT JOIN orders_item as oi
ON o.order_id = oi.order_id
LEFT JOIN products AS p
ON p.product_id = oi.product_id
GROUP BY c.customer_name
ORDER BY total_amount DESC
LIMIT 5;
-- Question 91
-- Display all orders along with the customer_name. Include orders even if customer details are missing.
SELECT c.customer_name , o.order_id
FROM customers AS c
RIGHT JOIN orders AS o
ON c.customer_id = o.customer_id;
-- Question 92
-- Display all orders and the city of the customer who placed them. Include orders that have no matching customer.
SELECT c.city , o.order_id
FROM customers AS c
RIGHT JOIN orders AS o
ON c.customer_id = o.customer_id;
-- Question 93
-- Display all order_items along with the product_name. Include order_items even if the product is missing from the products table.
SELECT oi.order_item_id,p.product_name
FROM products AS p
RIGHT JOIN orders_item AS oi
ON p.product_id = oi.product_id;
-- Question 94
-- Display all order_items along with the category of the product. Include order_items even if the product does not exist.
SELECT oi.order_item_id , p.category
FROM products AS p
RIGHT JOIN orders_item AS oi
ON p.product_id = oi.product_id;
-- Question 95
-- Display all orders along with the total quantity of products in each order. Include orders even if no products were added.
SELECT o.order_id , COALESCE(SUM(oi.quantity), 0)
AS total_quantity
FROM orders_item AS oi
RIGHT JOIN orders AS o
ON o.order_id = oi.order_id
GROUP BY o.order_id;
-- Question 96
-- Display all order_items along with the customer_name. Include order_items even if customer information is missing.
SELECT oi.order_item_id, c.customer_name
FROM orders_item AS oi
LEFT JOIN orders AS o
ON oi.order_id = o.order_id
LEFT JOIN customers AS c
ON o.customer_id = c.customer_id;
-- Question 97
-- Display all products and the order_id in which they were purchased. Include every order_item even if the product record is missing.
SELECT p.product_name, oi.order_id
FROM products AS p
RIGHT JOIN orders_item AS oi
ON p.product_id = oi.product_id;
-- Question 98
-- Display all orders along with the total revenue generated from each order. Include orders that have no order_items (show revenue as 0).
SELECT o.order_id,
COALESCE(SUM((p.unit_price * oi.quantity) - oi.discount), 0) AS total_revenue
FROM orders AS o
LEFT JOIN orders_item AS oi
ON o.order_id = oi.order_id
LEFT JOIN products AS p
ON oi.product_id = p.product_id
GROUP BY o.order_id;
-- Question 99
-- Display all order_items along with the order_status. Include order_items even if the order record is missing.
SELECT oi.order_item_id, o.order_status
FROM orders AS o
RIGHT JOIN orders_item AS oi
ON o.order_id = oi.order_id;
-- Question 100
-- Display all order_items along with the customer_name, product_name, quantity, and total amount for each item. Include every order_item even if related customer, order, or product information is missing.
SELECT oi.order_item_id, c.customer_name, p.product_name, oi.quantity,
COALESCE((p.unit_price * oi.quantity) - oi.discount, 0) AS total_amount
FROM orders_item AS oi
LEFT JOIN orders AS o
ON oi.order_id = o.order_id
LEFT JOIN customers AS c
ON o.customer_id = c.customer_id
LEFT JOIN products AS p
ON oi.product_id = p.product_id;
-- Question 101
-- Display the customer_name of customers whose age is greater than the average age of all customers.
SELECT customer_name , age
FROM customers
WHERE age > (
SELECT AVG(age)
FROM customers) ;
-- Question 102
-- Display the product_name and unit_price of the most expensive product.
SELECT product_name,unit_price
FROM products
WHERE unit_price =
(SELECT MAX(unit_price)
FROM products);
-- Question 103
-- Display the customer_name of customers who have placed at least one order.
SELECT DISTINCT c.customer_name
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id;
-- Question 104
-- Display the product_name of products that have never been ordered.
SELECT product_name
FROM products
WHERE product_id NOT IN
(SELECT product_id
FROM orders_item);
-- Question 105
-- Display the customer_name of customers who have spent more than the average amount spent by all customers.
WITH customer_spending AS
(SELECT c.customer_id, c.customer_name,
SUM((p.unit_price * oi.quantity) - oi.discount) AS total_spent
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON p.product_id = oi.product_id
GROUP BY c.customer_id, c.customer_name
)
SELECT customer_name, total_spent
FROM customer_spending
WHERE total_spent > (
SELECT AVG(total_spent)
FROM customer_spending
);
-- Question 106
-- Display the order_id whose total revenue is the highest.
SELECT oi.order_id,
SUM(p.unit_price * oi.quantity - oi.discount)
AS total_revenue
FROM orders AS o
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON oi.product_id = p.product_id
GROUP BY oi.order_id
ORDER BY total_revenue DESC
LIMIT 1;
-- Question 107
-- Display the product_name of products whose unit_price is greater than the average unit_price of their category.
SELECT product_name , unit_price
FROM products AS P
WHERE unit_price >
(SELECT AVG(unit_price)
FROM products
WHERE category = p.category) ;
-- Question 108
-- Display the customer_name of customers who purchased the most expensive product.
SELECT DISTINCT c.customer_name
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON oi.product_id = p.product_id
WHERE p.unit_price =
(SELECT MAX(unit_price)
FROM products);
-- Question 109
-- Display the city whose customers generated the highest total revenue.
SELECT c.city , SUM(p.unit_price * oi.quantity - oi.discount)
AS total_revenue
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON oi.product_id = p.product_id
GROUP BY city
ORDER BY total_revenue DESC
LIMIT 1 ;
-- Question 110
-- Display the customer_name and total amount spent by the customer who spent the highest amount.
SELECT c.customer_name, SUM(p.unit_price * oi.quantity)
AS total_amount
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN orders_item AS oi
ON o.order_id = oi.order_id
INNER JOIN products AS p
ON oi.product_id = p.product_id
GROUP BY customer_name
ORDER BY total_amount DESC
LIMIT 1 ;
-- Question 111
-- Display product_name, unit_price,
-- and create a column called price_category.
-- If unit_price >= 5000 → 'Expensive'
-- Otherwise → 'Affordable'
SELECT product_name , unit_price ,
CASE
WHEN unit_price >= 5000 THEN 'Expensive'
ELSE 'Affordable'
END AS price_category
FROM products;
-- Question 112
-- Display customer_name and age.
-- Create a column called age_group.
-- If age >= 60 → 'Senior'
-- If age >= 30 → 'Adult'
-- Otherwise → 'Young'
SELECT customer_name , age ,
CASE
WHEN age >=60 THEN 'Senior'
WHEN age >=30 THEN 'Adult'
ELSE 'Young'
END AS age_group
FROM customers;
-- Question 113
-- Display order_id and total_amount
-- (unit_price × quantity − discount).
-- Create a column called order_type.
-- If total_amount >= 10000 → 'Premium'
-- Otherwise → 'Regular'
SELECT oi.order_id , p.unit_price * oi.quantity - oi.discount
AS total_amount ,
CASE
WHEN (p.unit_price * oi.quantity - oi.discount) >= 10000 THEN 'Premium'
ELSE 'Regular'
END AS order_type
FROM products AS p
INNER JOIN orders_item as oi
ON p.product_id = oi.product_id ;
-- Question 114
-- Display product_name and unit_price.
-- Create a column called discount_category.