-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagist.sql
More file actions
83 lines (58 loc) · 2.2 KB
/
magist.sql
File metadata and controls
83 lines (58 loc) · 2.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
USE magist
#How many orders are there in the dataset? The orders table contains a row for each order, so this should be easy to find out!
SELECT count(*) AS orders_count
FROM orders;
#Are orders actually delivered?
SELECT order_status, COUNT(*) AS orders
FROM orders
GROUP BY order_status;
#Is Magist having user growth?
SELECT
YEAR(order_purchase_timestamp) AS year_,
MONTH(order_purchase_timestamp) AS month_,
COUNT(customer_id)
FROM
orders
GROUP BY year_ , month_
ORDER BY year_ , month_;
#How many products are there in the products table?
SELECT
COUNT(DISTINCT product_id) AS products_count
FROM
products;
#Which are the categories with most products?
SELECT
product_category_name,
COUNT(DISTINCT product_id) AS n_products
FROM
products
GROUP BY product_category_name
ORDER BY COUNT(product_id) DESC;
#How many of those products were present in actual transactions?
SELECT
count(DISTINCT product_id) AS n_products
FROM
order_items;
#What’s the price for the most expensive and cheapest products?
SELECT
MIN(price) AS cheapest,
MAX(price) AS most_expensive
FROM
order_items;
#What categories of tech products does Magist have?
SELECT t.product_category_name_english, MAX(t.product_category_name) AS translated_name, MAX(p.product_category_name) AS product_name
FROM product_category_name_translation as t
LEFT JOIN products as p
ON t.product_category_name = p.product_category_name
GROUP BY t.product_category_name_english;
SELECT t.product_category_name_english, ANY_VALUE(t.product_category_name) AS translated_name, ANY_VALUE(p.product_category_name) AS product_name
FROM products AS p
LEFT JOIN product_category_name_translation as t
ON p.product_category_name = t.product_category_name
GROUP BY t.product_category_name_english;
#How many products of these tech categories have been sold (within the time window of the database snapshot)?
SELECT t.product_category_name_english, ANY_VALUE(t.product_category_name) AS translated_name, ANY_VALUE(p.product_category_name) AS product_name
FROM products AS p
LEFT JOIN product_category_name_translation as t
ON p.product_category_name = t.product_category_name
GROUP BY t.product_category_name_english;