-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorthwind_queries.sql
179 lines (135 loc) · 4.56 KB
/
northwind_queries.sql
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
/*
This file contains some of the queries I used in creating the Metabase dashboard.
*/
-- Get the names and the quantities in stock for each product.
SELECT product_name, units_in_stock
FROM products;
-- Get a list of current products (Product ID and name).
SELECT product_id, product_name
FROM products;
-- Get a list of the most and least expensive products (name and unit price).
SELECT product_name, unit_price
FROM Products
WHERE unit_price > 100 OR unit_price < 5
ORDER BY unit_price DESC;
-- Get products that cost less than $20
SELECT product_name, unit_price
FROM products
WHERE unit_price < 20
ORDER BY unit_price ASC;
-- Get products that cost between $15 and $25.
SELECT product_name, unit_price
FROM products
WHERE unit_price BETWEEN 15 AND 25;
-- Get products above average price.
SELECT product_name, unit_price
FROM products
WHERE unit_price > (
SELECT AVG(unit_price)
FROM products
);
-- Find the ten most expensive products.
SELECT product_name, unit_price
FROM products
ORDER BY unit_price DESC
LIMIT 10;
-- Get a list of discontinued products (Product ID and name).
SELECT product_id, product_name, discontinued
FROM products
WHERE discontinued = 1;
-- Count current and discontinued products.
SELECT discontinued, COUNT(*)
FROM products
GROUP BY discontinued;
-- Find products with less units in stock than the quantity on order.
SELECT units_in_stock, units_in_order
FROM products
WHERE units_in_stock < units_in_order;
-- Find the customer who had the highest order amount
SELECT customer_id, COUNT(*)
FROM orders
GROUP BY customer_id
ORDER BY orders DESC
LIMIT 1;
-- Get orders for a given employee and the according customer
SELECT *
FROM orders
WHERE employee_id = 1;
-- Find the hiring age of each employee
SELECT employee_id, age(hire_date, birth_date) AS "hiring_age"
FROM employees;
-- Create views for some of these queries
CREATE VIEW employee_age AS (
SELECT employee_id, age(hire_date, birth_date) AS "hiring_age"
FROM employees
);
-- Create a view of all the relevant columns from all the tables
CREATE VIEW all_data AS
(SELECT
categories.category_id, categories.category_name,
products.product_id, products.product_name,products.unit_price,
order_details.order_id, order_details.quantity,
orders.customer_id, orders.order_date, orders.ship_country, orders.ship_city,
customers.city, customers.country
FROM categories
JOIN products on products.category_id = categories.category_id
JOIN order_details on order_details.product_id = products.product_id
JOIN orders on orders.order_id = order_details.order_id
JOIN customers on customers.customer_id = orders.customer_id
);
-- Mark products as expensive of inexpensive
SELECT product_id,
CASE
WHEN unit_price > 150 THEN 'expensive'
ELSE 'inexpensive' END
FROM order_details;
-- Calculate the average order value per country
SELECT
AVG(order_details.product_id * order_details.unit_price - order_details.discount) AS order_value,
orders.ship_country
FROM order_details
JOIN orders on orders.order_id = order_details.order_id
GROUP BY orders.ship_country
ORDER BY order_value DESC;
-- Calculate the total order value per country
SELECT
SUM(order_details.product_id * order_details.unit_price - order_details.discount) AS order_value,
orders.ship_country
FROM order_details
JOIN orders on orders.order_id = order_details.order_id
GROUP BY orders.ship_country
ORDER BY order_value DESC;
-- Calculate the average order value per customer
SELECT
AVG(order_details.product_id * order_details.unit_price - order_details.discount) AS order_value,
orders.customer_id
FROM order_details
JOIN orders on orders.order_id = order_details.order_id
GROUP BY orders.customer_id
ORDER BY order_value DESC;
-- Calculate the average order value by date
SELECT
AVG(order_details.product_id * order_details.unit_price - order_details.discount) AS order_value,
orders.order_date
FROM order_details
JOIN orders on orders.order_id = order_details.order_id
GROUP BY orders.order_date
ORDER BY order_value DESC;
-- Day with the highest order value
SELECT
MAX(order_details.product_id * order_details.unit_price - order_details.discount),
orders.order_date
FROM order_details
JOIN orders on orders.order_id = order_details.order_id
GROUP BY orders.order_date
ORDER BY orders.order_date DESC
LIMIT 1;
SELECT
order_details.order_id, SUM(order_details.quantity * order_details.unit_price) AS order_value,
orders.order_date, orders.ship_country
FROM order_details
JOIN orders on orders.order_id = order_details.order_id
GROUP BY
order_details.order_id,
orders.order_date,
orders.ship_country;