-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutivaSamuel.sql
More file actions
283 lines (255 loc) · 6.7 KB
/
utivaSamuel.sql
File metadata and controls
283 lines (255 loc) · 6.7 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
-- Name: John Samuel
-- Email: john96samuel@gmail.com
-- About: Utiva Data Science remote competition
-- Date: November 12, 2020
DROP DATABASE IF EXISTS utiva;
CREATE DATABASE IF NOT EXISTS utiva;
USE utiva;
DROP TABLE IF EXISTS breweries;
CREATE TABLE IF NOT EXISTS breweries (
sales_ID INT NOT NULL PRIMARY KEY,
sales_rep VARCHAR(40) NOT NULL,
emails VARCHAR(40) NOT NULL,
brands VARCHAR(40) NOT NULL,
plant_cost INT NOT NULL,
unit_price INT NOT NULL,
quantity INT NOT NULL,
cost INT NOT NULL,
profit INT NOT NULL,
countries VARCHAR(40) NOT NULL,
region VARCHAR(40) NOT NULL,
months VARCHAR(40) NOT NULL,
years INT NOT NULL
);
load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/International_Breweries (1).csv"
into table breweries
fields terminated by ","
enclosed by ""
lines terminated by "\n"
ignore 1 rows;
# LEGEND
-- Anglophone african countries are Ghana, Nigeria,
-- Francophone african countries are Togo, Benin, Senegal
-- most oil rich country in Africa is Nigeria
# SECTION A (Profit Analysis)
# (1)
-- Question: Within the space of the last three years, what was the profit worth of the breweries, inclusive of the anglophone and the francophone territories?
SELECT
SUM(profit) AS profit_worth
FROM
breweries;
# (2)
-- Question: Compare the total profit between these two territories in order for the territory manager, Mr. Stone make strategic decision that will aid profit maximization in 2020.
SELECT
r.territory, SUM(r.total_profit) AS total_profit
FROM
(SELECT
CASE
WHEN
t.countries = 'Ghana'
OR t.countries = 'Nigeria'
THEN
'Anglophone'
WHEN
t.countries = 'Togo'
OR t.countries = 'Benin'
OR t.countries = 'Senegal'
THEN
'Franchophone'
END AS territory,
t.total_profit AS total_profit
FROM
(SELECT
countries, SUM(profit) AS total_profit
FROM
breweries
GROUP BY countries) AS t) AS r
GROUP BY r.territory;
# (3)
-- Question: Country that generated the highest profit in 2019
SELECT
countries, SUM(profit) AS total_profit
FROM
breweries
WHERE
years = 2019
GROUP BY countries
ORDER BY total_profit DESC
LIMIT 1;
# (4)
-- Question: Help him find the year with the highest profit.
SELECT
years, SUM(profit) AS total_profit
FROM
breweries
GROUP BY years
ORDER BY total_profit DESC
LIMIT 1;
# (5)
-- Question: Which month in the three years were the least profit generated?
SELECT
months, SUM(profit) AS min_profit
FROM
breweries
GROUP BY months
ORDER BY min_profit
LIMIT 1;
# (6)
-- Question: What was the minimum profit in the month of December 2018?
SELECT
profit AS Dec_2018_least_profit
FROM
breweries
WHERE
years = 2018 AND months = 'December'
ORDER BY profit
LIMIT 1;
# (7)
-- Question: Compare the profit in percentage for each of the month in 2019
SET @gross_profit = (SELECT sum(profit) FROM breweries WHERE years = 2019);
SELECT @gross_profit;
SELECT
months, round(((sum(profit)/@gross_profit) * 100), 0) AS 2019_percentage_monthly_profit
FROM
breweries
WHERE
years = 2019
GROUP BY months
ORDER BY 2019_percentage_monthly_profit DESC;
# (8)
-- Question: Which particular brand generated the highest profit in Senegal?
SELECT
brands, SUM(profit) AS total_profit
FROM
breweries
WHERE countries = "Senegal"
GROUP BY brands
ORDER BY total_profit DESC
LIMIT 1;
# SECTION B (Brand analysis)
# (1)
-- Question: Within the last two years, the brand manager wants to know the top three brands consumed in the francophone countries
SELECT
brands, sum(quantity) AS total_quantity
FROM
breweries
WHERE
countries IN ('Togo' , 'Benin', 'Senegal')
AND years IN (2018 , 2019)
GROUP BY brands
ORDER BY total_quantity DESC
LIMIT 3;
# (2)
-- Question: Find out the top two choice of consumer brands in Ghana
SELECT
brands, sum(quantity) AS total_quantity
FROM
breweries
WHERE
countries = 'Ghana'
GROUP BY brands
ORDER BY total_quantity DESC
LIMIT 2;
# (3)
-- Question: Find out the details of beers consumed in the past three years in the most oil reach country in West Africa.
SELECT
brands, sum(quantity) AS total_quantity
FROM
breweries
WHERE
countries = 'Nigeria' AND brands NOT LIKE ('%malt')
GROUP BY brands
ORDER BY total_quantity DESC;
# (4)
-- Question: Favorites malt brand in Anglophone region between 2018 and 2019
SELECT
brands, sum(quantity) AS total_quantity
FROM
breweries
WHERE
brands LIKE ('%malt') AND countries IN ('Ghana', 'Nigeria') AND years IN (2018, 2019)
GROUP BY brands
ORDER BY total_quantity DESC
LIMIT 1;
# (5)
-- Question: Which brands sold the highest in 2019 in Nigeria?
SELECT
brands, sum(quantity) AS total_quantity
FROM
breweries
WHERE
countries = 'Nigeria' and years = 2019
GROUP BY brands
ORDER BY total_quantity DESC
LIMIT 1;
# (6)
-- Question: Favorites brand in South_South region in Nigeria
SELECT
brands, sum(quantity) AS total_quantity
FROM
breweries
WHERE
countries = 'Nigeria' AND region = 'southsouth'
GROUP BY brands
ORDER BY total_quantity DESC
LIMIT 1;
# (7)
-- Question: Bear consumption in Nigeria
SELECT
sum(quantity) AS total_beer_consumption
FROM
breweries
WHERE
countries = 'Nigeria' AND brands NOT LIKE ('%malt');
# (8)
-- Question: Level of consumption of Budweiser in the regions in Nigeria
SELECT
region, sum(quantity) AS total_quantity
FROM
breweries
WHERE
countries = 'Nigeria' AND brands = 'budweiser'
GROUP BY region
ORDER BY total_quantity DESC;
# (9)
-- Question: Level of consumption of Budweiser in the regions in Nigeria in 2019 (Decision on Promo)
SELECT
region, sum(quantity) AS total_quantity
FROM
breweries
WHERE
countries = 'Nigeria' AND brands = 'budweiser' AND years = 2019
GROUP BY region
ORDER BY total_quantity DESC;
# SECTION C (Contries Analysis)
# (1)
-- Question: Country with the highest consumption of beer.
SELECT
countries, sum(quantity) AS total_quantity
FROM
breweries
GROUP BY countries
ORDER BY total_quantity DESC
LIMIT 1;
# (2)
-- Question: Highest sales personnel of Budweiser in Senegal
SELECT
sales_rep, sum(profit) AS total_income
FROM
breweries
WHERE
countries = 'Senegal' AND brands = 'budweiser'
GROUP BY sales_rep
ORDER BY total_income DESC
LIMIT 1;
# (3)
-- Question: Country with the highest profit of the fourth quarter in 2019
SELECT
countries, sum(profit) AS total_income
FROM
breweries
WHERE
months IN ('October', 'November', 'December') AND years = 2019
GROUP BY countries
ORDER BY total_income DESC
LIMIT 1;