-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASSINGMENT03.sql
More file actions
192 lines (154 loc) · 3.62 KB
/
Copy pathASSINGMENT03.sql
File metadata and controls
192 lines (154 loc) · 3.62 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
-- ASSIGNMENT 03 — GROUP BY, HAVING & SUBQUERIES
-- Database : BikeStores
-- Topics : GROUP BY · Aggregate Functions · HAVING
-- Subqueries · JOINs with GROUP BY
-- QUESTION ;1
SELECT
customer_id,
count(order_id) as order_count
from
sales.orders
GROUP by
customer_id
ORDER by
order_count DESC;
-- QUESTION ;2
SELECT
store_id,
count(order_id) as total_orders
from
sales.orders
group by
store_id
-- QUESTION ;3
-- -- Net revenue formula: SUM( quantity * list_price * (1 - discount) )
SELECT
order_id,
SUM(quantity * list_price * (1 - discount)) AS net_revenue
FROM sales.order_items
GROUP BY order_id
ORDER BY net_revenue DESC;
-- QUESTION ;4
-- (rounded to 2 decimal places).
-- (Hint: use ROUND())
select
category_id,
ROUND(AVG(list_price), 2) AS avg_price
from
production.products
group by
category_id
-- QUESTION ;5
SELECT
YEAR(order_date) AS order_year,
COUNT(order_id) AS total_orders
FROM sales.orders
GROUP BY YEAR(order_date)
ORDER BY order_year;
-- QUESTION ;6
--SELECT
-- CUSTOMER_ID,
-- COUNT(ORDER_ID) AS ORDER_COUNT
--FROM
-- SALES.ORDERS
--GROUP BY
-- CUSTOMER_ID
--HAVING
-- COUNT(CUSTOMER_ID) >5
-- there is no customers who have placed MORE than 5 orders in total
SELECT
customer_id,
COUNT(order_id) AS order_count
FROM sales.orders
GROUP BY customer_id
ORDER BY order_count DESC;
SELECT
customer_id,
COUNT(order_id) AS order_count
FROM sales.orders
GROUP BY customer_id
HAVING COUNT(order_id) >= 2;
-- QUESTION ;7
SELECT
category_id,
ROUND(AVG(list_price), 2) AS avg_price
FROM production.products
GROUP BY category_id
HAVING AVG(list_price) > 1500;
-- QUESTION 8
SELECT
customer_id,
YEAR(order_date) AS order_year,
COUNT(order_id) AS order_count
FROM sales.orders
WHERE YEAR(order_date) = 2017
GROUP BY customer_id, YEAR(order_date)
HAVING COUNT(order_id) >= 2;
--question 9
SELECT *
FROM sales.orders
WHERE customer_id IN (
SELECT customer_id
FROM sales.customers
WHERE city = 'Houston'
);
-- question 10
SELECT
product_name,
list_price
FROM production.products
WHERE list_price > (
SELECT AVG(list_price)
FROM production.products
);
-- QUESTION ;11
SELECT
product_name,
list_price
FROM production.products
WHERE category_id IN (
SELECT category_id
FROM production.categories
WHERE category_name IN ('Mountain Bikes', 'Road Bikes')
);
-- QUESTION ;12
--SELECT
-- customer_id,
-- first_name,
-- last_name
--FROM sales.customers
--WHERE customer_id NOT IN (
-- SELECT customer_id
-- FROM sales.orders
--);
-- its dost's work
-- QUESTION ;13
SELECT
c.city,
COUNT(o.order_id) AS total_orders
FROM sales.orders o
INNER JOIN sales.customers c
ON o.customer_id = c.customer_id
GROUP BY c.city
ORDER BY total_orders DESC;
--QUESTION ;14
SELECT
s.first_name + ' ' + s.last_name AS staff_name,
COUNT(o.order_id) AS order_count
FROM sales.staffs s
INNER JOIN sales.orders o
ON s.staff_id = o.staff_id
GROUP BY s.first_name, s.last_name
ORDER BY order_count DESC;
-- QUESTION ;15
SELECT
c.first_name + ' ' + c.last_name AS customer_name,
SUM(oi.quantity * oi.list_price * (1 - oi.discount)) AS total_spent
FROM sales.customers c
INNER JOIN sales.orders o
ON c.customer_id = o.customer_id
INNER JOIN sales.order_items oi
ON o.order_id = oi.order_id
GROUP BY c.first_name, c.last_name
HAVING SUM(oi.quantity * oi.list_price * (1 - oi.discount)) > 10000
ORDER BY total_spent DESC;