Skip to content

Commit 382ab17

Browse files
committed
Unleasing the power with Subqueries
1 parent 194ec89 commit 382ab17

File tree

1 file changed

+372
-0
lines changed

1 file changed

+372
-0
lines changed
Lines changed: 372 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
1+
/*
2+
Find the average number of events for each day for each channel.
3+
*/
4+
5+
SELECT channel,
6+
AVG(num_events) avg_num_events
7+
FROM (
8+
SELECT channel,
9+
DATE_TRUNC('day', occurred_at),
10+
COUNT(*) num_events
11+
FROM web_events
12+
GROUP BY 1, 2
13+
) sub_table
14+
GROUP BY channel;
15+
16+
17+
18+
19+
20+
/*
21+
Use DATE_TRUNC to pull "month" level information about the first order
22+
ever placed in the orders table. Use this result to find only the orders
23+
that took place in the same month-year combo as the first order and then
24+
pull the average for each type of paper 'qty' in this month and the total
25+
amount spent on all orders.
26+
*/
27+
28+
SELECT AVG(standard_qty) standard,
29+
AVG(gloss_qty) gloss,
30+
AVG(poster_qty) poster,
31+
SUM(total_amt_usd)
32+
FROM orders
33+
WHERE DATE_TRUNC('month', occurred_at) =
34+
(SELECT MIN(DATE_TRUNC('month', occurred_at))
35+
FROM orders)
36+
37+
38+
39+
40+
41+
/*
42+
Provide the name of the sales_rep in each region with the largest amount
43+
of total_amt_usd sales.
44+
*/
45+
46+
SELECT sub2.region,
47+
sub1.sales_rep,
48+
sub2.total_amt_usd
49+
FROM (
50+
SELECT r.name region,
51+
s.name sales_rep,
52+
SUM(o.total_amt_usd) total_amt_usd
53+
FROM orders o
54+
JOIN accounts a
55+
ON a.id = o.account_id
56+
JOIN sales_reps s
57+
ON s.id = a.sales_rep_id
58+
JOIN region r
59+
ON r.id = s.region_id
60+
GROUP BY 1, 2
61+
) sub1
62+
JOIN (
63+
SELECT region,
64+
MAX(total_amt_usd) total_amt_usd
65+
FROM (
66+
SELECT r.name region,
67+
s.name sales_rep,
68+
SUM(o.total_amt_usd) total_amt_usd
69+
FROM orders o
70+
JOIN accounts a
71+
ON a.id = o.account_id
72+
JOIN sales_reps s
73+
ON s.id = a.sales_rep_id
74+
JOIN region r
75+
ON r.id = s.region_id
76+
GROUP BY 1, 2
77+
) sub
78+
GROUP BY 1
79+
) sub2
80+
ON sub1.region = sub2.region
81+
AND sub1.total_amt_usd = sub2.total_amt_usd
82+
ORDER BY 3;
83+
84+
85+
86+
87+
88+
/*
89+
For the region with the largest (sum) of sales total_amt_usd, how many
90+
total (count) orders were placed?
91+
*/
92+
93+
SELECT r.name region,
94+
SUM(o.total_amt_usd) total_amt_usd,
95+
COUNT(*)
96+
FROM orders o
97+
JOIN accounts a
98+
ON a.id = o.account_id
99+
JOIN sales_reps s
100+
ON s.id = a.sales_rep_id
101+
JOIN region r
102+
ON r.id = s.region_id
103+
GROUP BY 1
104+
ORDER BY 2 DESC
105+
LIMIT 1;
106+
107+
108+
109+
110+
111+
/*
112+
For the name of the account that purchased the most (in total over their
113+
lifetime as a customer) standard_qty paper, how many accounts still had
114+
more in total purchases?
115+
*/
116+
117+
SELECT COUNT(*)
118+
FROM (
119+
SELECT a.name, SUM(o.total)
120+
FROM accounts a
121+
JOIN orders o
122+
ON o.account_id = a.id
123+
GROUP BY a.name
124+
HAVING SUM(o.total) > (
125+
SELECT total
126+
FROM (
127+
SELECT SUM(o.standard_qty) standard_qty,
128+
SUM(o.total) total
129+
FROM orders o
130+
GROUP BY account_id
131+
ORDER BY 1 DESC
132+
LIMIT 1
133+
)sub1
134+
)
135+
)sub;
136+
137+
138+
139+
140+
141+
/*
142+
For the customer that spent the most (in total over their lifetime as a
143+
customer) total_amt_usd, how many web_events did they have for each
144+
channel?
145+
*/
146+
147+
SELECT channel,
148+
w.account_id,
149+
COUNT(*)
150+
FROM web_events w
151+
JOIN (
152+
SELECT o.account_id, SUM(o.total_amt_usd)
153+
FROM orders o
154+
GROUP BY 1
155+
ORDER BY 2 DESC
156+
LIMIT 1
157+
) sub
158+
ON sub.account_id = w.account_id
159+
GROUP BY 1, 2
160+
ORDER BY 3 DESC;
161+
162+
163+
164+
165+
166+
/*
167+
What is the lifetime average amount spent in terms of total_amt_usd for
168+
the top 10 total spending accounts?
169+
*/
170+
171+
SELECT AVG(total)
172+
FROM (
173+
SELECT account_id,
174+
SUM(total_amt_usd) total
175+
FROM orders
176+
GROUP BY 1
177+
ORDER BY 2 DESC
178+
LIMIT 10
179+
) sub;
180+
181+
182+
183+
184+
185+
/*
186+
What is the lifetime average amount spent in terms of total_amt_usd for
187+
only the companies that spent more than the average of all orders.
188+
*/
189+
190+
SELECT AVG(total_amt_usd)
191+
FROM (
192+
SELECT account_id,
193+
AVG(total_amt_usd) total_amt_usd
194+
FROM orders
195+
GROUP BY account_id
196+
) sub
197+
WHERE total_amt_usd >
198+
(SELECT AVG(total_amt_usd) total_amt_usd
199+
FROM orders)
200+
201+
202+
203+
204+
205+
/*
206+
Provide the name of the sales_rep in each region with the largest amount
207+
of total_amt_usd sales.
208+
*/
209+
210+
WITH
211+
sub1 AS (
212+
SELECT r.name region,
213+
s.name sales_rep,
214+
SUM(o.total_amt_usd) total_amt_usd
215+
FROM orders o
216+
JOIN accounts a
217+
ON a.id = o.account_id
218+
JOIN sales_reps s
219+
ON s.id = a.sales_rep_id
220+
JOIN region r
221+
ON r.id = s.region_id
222+
GROUP BY 1, 2
223+
),
224+
225+
sub2 AS (
226+
SELECT region,
227+
MAX(total_amt_usd) total_amt_usd
228+
FROM sub1
229+
GROUP BY 1
230+
)
231+
232+
SELECT sub2.region,
233+
sub1.sales_rep,
234+
sub2.total_amt_usd
235+
FROM sub1
236+
JOIN sub2
237+
ON sub1.region = sub2.region
238+
AND sub1.total_amt_usd = sub2.total_amt_usd
239+
ORDER BY 3;
240+
241+
242+
243+
244+
245+
/*
246+
For the region with the largest (sum) of sales total_amt_usd, how many
247+
total (count) orders were placed?
248+
*/
249+
250+
SELECT r.name region,
251+
SUM(o.total_amt_usd) total_amt_usd,
252+
COUNT(*)
253+
FROM orders o
254+
JOIN accounts a
255+
ON a.id = o.account_id
256+
JOIN sales_reps s
257+
ON s.id = a.sales_rep_id
258+
JOIN region r
259+
ON r.id = s.region_id
260+
GROUP BY 1
261+
ORDER BY 2 DESC
262+
LIMIT 1;
263+
264+
265+
266+
267+
268+
/*
269+
For the name of the account that purchased the most (in total over their
270+
lifetime as a customer) standard_qty paper, how many accounts still had
271+
more in total purchases?
272+
*/
273+
274+
WITH
275+
sub1 AS (
276+
SELECT SUM(o.standard_qty) standard_qty,
277+
SUM(o.total) total
278+
FROM orders o
279+
GROUP BY account_id
280+
ORDER BY 1 DESC
281+
LIMIT 1
282+
),
283+
284+
sub2 AS (
285+
SELECT a.name, SUM(o.total)
286+
FROM accounts a
287+
JOIN orders o
288+
ON o.account_id = a.id
289+
GROUP BY a.name
290+
HAVING SUM(o.total) > (SELECT total FROM sub1)
291+
)
292+
293+
SELECT COUNT(*)
294+
FROM sub2;
295+
296+
297+
298+
299+
300+
/*
301+
For the customer that spent the most (in total over their lifetime as a
302+
customer) total_amt_usd, how many web_events did they have for each
303+
channel?
304+
*/
305+
306+
WITH
307+
sub AS (
308+
SELECT o.account_id, SUM(o.total_amt_usd)
309+
FROM orders o
310+
GROUP BY 1
311+
ORDER BY 2 DESC
312+
LIMIT 1
313+
)
314+
315+
SELECT channel,
316+
w.account_id,
317+
COUNT(*)
318+
FROM web_events w
319+
JOIN sub
320+
ON sub.account_id = w.account_id
321+
GROUP BY 1, 2
322+
ORDER BY 3 DESC;
323+
324+
325+
326+
327+
328+
/*
329+
What is the lifetime average amount spent in terms of total_amt_usd
330+
for the top 10 total spending accounts?
331+
*/
332+
333+
WITH
334+
sub AS (
335+
SELECT account_id,
336+
SUM(total_amt_usd) total
337+
FROM orders
338+
GROUP BY 1
339+
ORDER BY 2 DESC
340+
LIMIT 10
341+
)
342+
343+
SELECT AVG(total)
344+
FROM sub;
345+
346+
347+
348+
349+
350+
/*
351+
What is the lifetime average amount spent in terms of total_amt_usd
352+
for only the companies that spent more than the average of all orders.
353+
*/
354+
355+
WITH
356+
sub AS (
357+
SELECT account_id,
358+
AVG(total_amt_usd) total_amt_usd
359+
FROM orders
360+
GROUP BY account_id
361+
)
362+
363+
SELECT AVG(total_amt_usd)
364+
FROM sub
365+
WHERE total_amt_usd >
366+
(SELECT AVG(total_amt_usd) total_amt_usd
367+
FROM orders)
368+
369+
370+
371+
372+

0 commit comments

Comments
 (0)