1
+ Problem Statement:
2
+ Get the product_id and the number of products ordered for each product_id.
3
+
4
+ Information about the tables:
5
+ Given below is a database of a newly established e- commerce website.
6
+ The database contains multiple tables i .e . products, orders, and transactions.
7
+ The information about required tables is given below
8
+
9
+ Table product:
10
+ Attributes list:
11
+ Attributes | Data Type
12
+ item_id | INT (PRIMARY KEY )
13
+ product_id | INT
14
+ pname | VARCHAR (225 )
15
+ instock | CHAR
16
+ price | DOUBLE
17
+ arrival_days | INT
18
+ seller | VARCHAR (225 )
19
+ youSave | DOUBLE
20
+
21
+ Data list:
22
+ + -- -------+------------+-------------------------------------------------------------------------+---------+----------+--------------+----------------------------+---------+
23
+ | item_id | product_id | p_name | inStock | price | arrival_days | seller | youSave |
24
+ + -- -------+------------+-------------------------------------------------------------------------+---------+----------+--------------+----------------------------+---------+
25
+ | 1 | 7556 | Iphone 11 (128GB) | N | 51300 .05 | 5 | Maple Store | 3600 |
26
+ | 2 | 2223 | optimum whey protien | Y | 1614 | 7 | Cloudtail India | 245 |
27
+ | 3 | 7556 | Iphone 11 (128GB) | Y | 53500 | 4 | Kukreja Telecom Store | 1400 |
28
+ | 4 | 6754 | Inalsa Electric Kettle Absa (Black/ Silver) | Y | 591 | 9 | k3stores | 10040 |
29
+ | 5 | 99 | Mini organising tech kit | Y | 1400 | 2 | dailyen | 200 |
30
+ | 6 | 7777 | OnePlus 9 Pro 5G (256GB) | Y | 65999 | 2 | Darshita Etel | 4000 |
31
+ | 7 | 5454 | Iphone 11 (64GB) | Y | 46999 | 3 | Maple Store | 3000 |
32
+ | 8 | 4324 | Women' s Cotton Blend Straight Kurti with Palazzos | Y | 549 | 10 | Maxx Store | 1000 |
33
+ | 9 | 5655 | Redmi 9 Activ (128GB) | N | 10999 | 3 | Darshita Electronics | 200 |
34
+ | 10 | 9753 | OnePlus Buds Z | N | 2999 | 3 | Appario Retail Private Ltd | 191 |
35
+ | 11 | 7777 | OnePlus 9 Pro 5G (256GB) | Y | 65999 | 2 | Darshita Etel | 4000 |
36
+ | 12 | 6477 | Ikigai: The Japanese secret to a long and happy life | Y | 427 | 3 | Cloudtail India | 123 |
37
+ | 13 | 7556 | Iphone 11 (128GB) | N | 51300.05 | 5 | Maple Store | 3600 |
38
+ | 14 | 1111 | Solimo Trance High Back Mesh Contemporary Office Chair (Black) | Y | 7599 | 4 | Cloudtail India | 5901 |
39
+ | 15 | 2222 | Nayasa 2 in 1 Dustbin - Dry Waste and Wet Waste Dustbin (33 Ltrs) - Big | Y | 1339 | 7 | Cloudtail India | 189 |
40
+ | 16 | 5555 | Ben Martin Men' s Relaxed Jeans | N | 698 | 12 | Maxx Store | 100 |
41
+ | 17 | 7777 | OnePlus 9 Pro 5G (256GB) | Y | 65999 | 2 | Darshita Etel | 4000 |
42
+ | 18 | 6754 | Inalsa Electric Kettle Absa (Black/ Silver) | Y | 591 | 9 | k3stores | 10040 |
43
+ | 19 | 2223 | optimum whey protien | Y | 1614 | 7 | Cloudtail India | 245 |
44
+ | 20 | 529 | MI Mobile cover | Y | 999 | 5 | dailyen | 500 |
45
+ | 21 | 5655 | Redmi 9 Activ (128GB) | N | 10999 | 3 | Darshita Electronics | 200 |
46
+ | 22 | 7556 | Iphone 11 (128GB) | Y | 54900 | 7 | Sunil mobile Store | 0 |
47
+ | 23 | 9753 | OnePlus Buds Z | N | 4000 | 5 | sekhri telecoms | 200 |
48
+ | 24 | 5454 | Iphone 11 (64GB) | Y | 43999 | 7 | imagine store | 6000 |
49
+ + -- -------+------------+-------------------------------------------------------------------------+---------+----------+--------------+----------------------------+---------+
50
+
51
+ Table orders:
52
+ Attributes list:
53
+ Attributes | Data Type
54
+ order_id | INT (PRIMARY KEY )
55
+ product_id | INT
56
+ customer_id | INT
57
+ payment_date | DATE
58
+ est_delivery_date | DATE
59
+ transaction_days | INT
60
+
61
+ Data list:
62
+ + -- --------+------------+-------------+----------------+--------------+-------------------+----------------+
63
+ | order_id | product_id | customer_id | checkout_price | payment_date | est_delivery_date | transaction_id |
64
+ + -- --------+------------+-------------+----------------+--------------+-------------------+----------------+
65
+ | oid1 | 7777 | cid3 | 63999 | 2021 - 11 - 12 | 2021 - 11 - 14 | tid1 |
66
+ | oid2 | 7556 | cid1 | 51300 | 2021 - 11 - 09 | 2021 - 11 - 13 | tid2 |
67
+ | oid3 | 6754 | cid5 | 591 | 2021 - 11 - 24 | 2021 - 12 - 03 | tid3 |
68
+ | oid4 | 7556 | cid3 | 51300 | 2021 - 11 - 10 | 2021 - 11 - 14 | tid4 |
69
+ | oid5 | 6477 | cid2 | 500 | 2021 - 11 - 06 | 2021 - 11 - 09 | tid5 |
70
+ | oid6 | 2223 | cid7 | 1514 | 2021 - 11 - 27 | 2021 - 12 - 04 | tid6 |
71
+ + -- --------+------------+-------------+----------------+--------------+-------------------+----------------+
72
+
73
+ Information about the table transactions
74
+ Attributes list:
75
+ Attributes | Data Type
76
+ transaction_id | VARCHAR (20 ) (PRIMARY KEY )
77
+ transaction_status | VARCHAR (25 )
78
+
79
+ Data list:
80
+ + -- --------------+--------------------+------------------+
81
+ | transaction_id | transaction_status | transaction_type |
82
+ + -- --------------+--------------------+------------------+
83
+ | tid1 | completed | online |
84
+ | tid2 | not completed | cod |
85
+ | tid3 | not completed | cod |
86
+ | tid4 | completed | online |
87
+ | tid5 | completed | online |
88
+ | tid6 | not completed | cod |
89
+ + -- --------------+--------------------+------------------+
90
+
91
+ Table customer:
92
+ Attributes list:
93
+ Attributes | Data Type
94
+ customer_id | VARCHAR (20 ) (PRIMARY KEY )
95
+ customer_name | VARCHAR (25 )
96
+ phone_no | INT
97
+ city | VARCHAR (200 )
98
+ pin_code | INT
99
+
100
+ Data list:
101
+ + -- -----------+-----------------+-----------+-----------+----------+
102
+ | customer_id | customer_name | phone_no | city | pin_code |
103
+ + -- -----------+-----------------+-----------+-----------+----------+
104
+ | cid1 | Riya Khanna | 987655443 | Delhi | 110005 |
105
+ | cid2 | Sahil Kumar | 987657643 | Mumbai | 230532 |
106
+ | cid3 | Vishwas Aanand | 987658871 | Kolkata | 700007 |
107
+ | cid4 | Harleen Kaur | 987677585 | Bengaluru | 560006 |
108
+ | cid5 | Priyanshu Gupta | 956758556 | Hyderabad | 500012 |
109
+ | cid6 | Lokesh Ravaliya | 998295561 | Bikaner | 334003 |
110
+ | cid7 | Kuldeep Daga | 997757204 | Mandsaur | 458001 |
111
+ | cid8 | Vaibhav Jain | 987677757 | Jalandhar | 144002 |
112
+ | cid9 | Lokesh Gupta | 956758500 | Hyderabad | 500013 |
113
+ + -- -----------+-----------------+-----------+-----------+----------+
114
+
115
+ Solution:
116
+ SELECT product_id, COUNT (product_id)
117
+ FROM orders
118
+ GROUP BY product_id;
119
+
120
+ Output:
121
+ + -- ----------+-------------------+
122
+ | product_id | COUNT (product_id) |
123
+ + -- ----------+-------------------+
124
+ | 7777 | 1 |
125
+ | 7556 | 2 |
126
+ | 6754 | 1 |
127
+ | 6477 | 1 |
128
+ | 2223 | 1 |
129
+ + -- ----------+-------------------+
0 commit comments