Skip to content

Commit e638d4c

Browse files
authored
Update Challenge 5 - Pub pricing analysis.md
1 parent 6175206 commit e638d4c

File tree

1 file changed

+127
-12
lines changed

1 file changed

+127
-12
lines changed

Challenge 5 - Pub pricing analysis.md

Lines changed: 127 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,36 +111,151 @@ VALUES
111111
## Ad-Hoc Questions and Solutions
112112
###1. How many pubs are located in each country?
113113

114-
``` SQL code ```
114+
``` sql
115+
SELECT country,
116+
COUNT(*) as num_pubs
117+
FROM pubs
118+
GROUP BY country;
119+
```
115120

116121
### 2. What is the total sales amount for each pub, including the beverage price and quantity sold?
117-
``` SQL code ```
122+
``` sql
123+
SELECT p.pub_id,
124+
p.pub_name, p.city,
125+
SUM(s.quantity * b.price_per_unit) as total_sales_amount
126+
FROM pubs p
127+
JOIN sales s ON p.pub_id = s.pub_id
128+
JOIN beverages b ON s.beverage_id = b.beverage_id
129+
GROUP BY p.pub_id, p.pub_name, p.city;
130+
```
118131

119132
### 3. Which pub has the highest average rating?
120133

121-
``` SQL code ```
134+
``` sql
135+
SELECT p.pub_id, p.pub_name, p.city,
136+
ROUND(AVG(r.rating),2) as avg_rating
137+
FROM pubs p
138+
JOIN ratings r ON p.pub_id = r.pub_id
139+
GROUP BY p.pub_id, p.pub_name
140+
ORDER BY avg_rating DESC
141+
LIMIT 1;
142+
```
122143

123144
### 4. What are the top 5 beverages by sales quantity across all pubs?
124145

125-
``` SQL code ```
146+
``` sql
147+
SELECT b.beverage_name,
148+
b.category,
149+
SUM(s.quantity) as total_quantity_sold
150+
FROM sales s
151+
JOIN beverages b ON s.beverage_id = b.beverage_id
152+
GROUP BY b.beverage_name, b.category
153+
ORDER BY total_quantity_sold DESC
154+
LIMIT 5;
155+
```
126156
### 5. How many sales transactions occurred on each date?
127157

128-
``` SQL code ```
158+
``` sql
159+
SELECT transaction_date,
160+
COUNT(*) as num_transactions
161+
FROM sales
162+
GROUP BY transaction_date;
163+
```
129164
### 6. Find the name of someone who had cocktails and which pub they had it in.
130165

131-
``` SQL code ```
166+
``` sql
167+
SELECT r.customer_name, p.pub_name
168+
FROM ratings r
169+
JOIN pubs p ON r.pub_id = p.pub_id
170+
JOIN sales s ON p.pub_id = s.pub_id
171+
JOIN beverages b ON s.beverage_id = b.beverage_id
172+
WHERE b.category = 'Cocktail'
173+
LIMIT 1;
174+
```
132175
### 7. What is the average price per unit for each category of beverages, excluding the category 'Spirit'?
133176

134-
``` SQL code ```
177+
``` sql
178+
SELECT category,
179+
ROUND( AVG(price_per_unit),2) as avg_price_per_unit
180+
FROM beverages
181+
WHERE category != 'Spirit'
182+
GROUP BY category;
183+
```
135184
### 8. Which pubs have a rating higher than the average rating of all pubs?
136185

137-
### ``` SQL code ```
138-
9. What is the running total of sales amount for each pub, ordered by the transaction date?
186+
```sql
187+
SELECT p.pub_id,
188+
p.pub_name,
189+
p.city,
190+
ROUND(AVG(r.rating),2) as avg_rating
191+
FROM pubs p
192+
JOIN ratings r ON p.pub_id = r.pub_id
193+
GROUP BY p.pub_id, p.pub_name, p.city, p.state, p.country
194+
HAVING AVG(r.rating) > (SELECT AVG(rating) FROM ratings);
195+
```
196+
### 9. What is the running total of sales amount for each pub, ordered by the transaction date?
139197

140-
``` SQL code ```
198+
``` sql
199+
SELECT p.pub_id, p.pub_name, p.city, p.state, p.country,
200+
s.transaction_date,
201+
SUM(b.price_per_unit * s.quantity)
202+
OVER (PARTITION BY p.pub_id ORDER BY s.transaction_date) as running_total
203+
FROM pubs p
204+
JOIN sales s ON p.pub_id = s.pub_id
205+
JOIN beverages b ON s.beverage_id = b.beverage_id
206+
ORDER BY p.pub_id, s.transaction_date;
207+
```
141208
### 10. For each country, what is the average price per unit of beverages in each category, and what is the overall average price per unit of beverages across all categories?
142209

143-
``` SQL code ```
210+
```sql
211+
SELECT
212+
b.beverage_name AS Beverage_Name,
213+
p.country AS Country,
214+
b.category AS Category,
215+
ROUND(AVG(b.price_per_unit), 2) AS Avg_Price,
216+
ROUND((SELECT AVG(price_per_unit) FROM beverages), 2) AS Overall_Average_Price_Per_Unit
217+
FROM pubs AS p
218+
LEFT JOIN sales AS s
219+
ON s.pub_id = p.pub_id
220+
LEFT JOIN beverages AS b
221+
ON b.beverage_id = s.beverage_id
222+
GROUP BY Beverage_Name, Country, Category;
223+
```
144224
### 11. For each pub, what is the percentage contribution of each category of beverages to the total sales amount, and what is the pub's overall sales amount?
145225

146-
``` SQL code ```
226+
```sql
227+
WITH PubCategorySales AS (
228+
SELECT
229+
p.pub_id,
230+
p.pub_name,
231+
b.category,
232+
SUM(b.price_per_unit * s.quantity) as total_category_sales
233+
FROM pubs p
234+
JOIN sales s ON p.pub_id = s.pub_id
235+
JOIN beverages b ON s.beverage_id = b.beverage_id
236+
GROUP BY p.pub_id, p.pub_name, b.category
237+
),
238+
239+
PubTotalSales AS (
240+
SELECT
241+
p.pub_id,
242+
SUM(b.price_per_unit * s.quantity) as total_sales_amount
243+
FROM pubs p
244+
JOIN sales s ON p.pub_id = s.pub_id
245+
JOIN beverages b ON s.beverage_id = b.beverage_id
246+
GROUP BY p.pub_id
247+
)
248+
249+
SELECT
250+
p.pub_id,
251+
p.pub_name,
252+
p.city,
253+
p.state,
254+
p.country,
255+
pcs.category,
256+
ROUND((pcs.total_category_sales / pts.total_sales_amount) * 100, 2) as category_contribution_percentage
257+
FROM pubs p
258+
JOIN PubCategorySales pcs ON p.pub_id = pcs.pub_id
259+
JOIN PubTotalSales pts ON p.pub_id = pts.pub_id
260+
ORDER BY p.pub_id, pcs.category;
261+
```

0 commit comments

Comments
 (0)