-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplore_queries.sql
More file actions
76 lines (64 loc) · 2.61 KB
/
Copy pathexplore_queries.sql
File metadata and controls
76 lines (64 loc) · 2.61 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
/*
These are simply exploratory queries for a better understanding.
*/
SELECT
c.category_id,
c.category_name,
SUM(oi.quantity * p.list_price * (1 - oi.discount)) AS total_revenue
FROM
production.categories c
JOIN -- NOTE: The highest in total revenues based on category are Mountain Bikes, would be helpful to understand our focus.
production.products p ON c.category_id = p.category_id -- Lowest would be Children Bicycles, maybe we could understand a little bit more about targeted demographic here.
JOIN
sales.order_items oi ON p.product_id = oi.product_id
GROUP BY
c.category_id, c.category_name
ORDER BY
total_revenue DESC;
SELECT
c.category_id,
c.category_name,
AVG(oi.discount) AS avg_discount_percentage
FROM
production.categories c
JOIN
production.products p ON c.category_id = p.category_id -- NOTE: Although there is not that much difference here for average discount percentage sorted by categories,
JOIN -- we could start thinking about strategically discounting items to drive sales incentives?
sales.order_items oi ON p.product_id = oi.product_id
GROUP BY
c.category_id, c.category_name
ORDER BY
avg_discount_percentage DESC;
SELECT
c.category_id,
c.category_name,
MONTHNAME(o.order_date) AS month,
SUM(oi.quantity) AS total_quantity_sold
FROM
production.categories c
JOIN
production.products p ON c.category_id = p.category_id
JOIN
sales.order_items oi ON p.product_id = oi.product_id
JOIN
sales.orders o ON oi.order_id = o.order_id
WHERE
o.order_date BETWEEN '2016-01-01' AND '2017-01-01' -- This is only in the year of 2016, feel free to remove to have the total duration.
GROUP BY
c.category_id, c.category_name, MONTH(o.order_date)
ORDER BY
total_quantity_sold DESC, category_name;
SELECT
c.category_id,
c.category_name,
p.product_id,
p.product_name,
ps.quantity AS current_stock
FROM
production.categories c -- Simple inventory could be beneficial to understand. For instance, maybe the top items don't sell very well.
JOIN
production.products p ON c.category_id = p.category_id
JOIN
production.stocks ps ON p.product_id = ps.product_id
ORDER BY
current_stock DESC;