-
Notifications
You must be signed in to change notification settings - Fork 0
/
HAVING.sql
103 lines (79 loc) · 2.27 KB
/
HAVING.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
-- How many of the sales reps have more than 5 accounts that they manage?
SELECT s.name, COUNT(a.name)
FROM sales_reps s
JOIN accounts a
ON s.id = a.sales_rep_id
GROUP by s.name
HAVING COUNT(a.name) > 5;
-- How many accounts have more than 20 orders?
SELECT a.name, COUNT(o.id)
FROM accounts a
JOIN orders o
ON a.id = o.account_id
GROUP by a.name
HAVING COUNT(o.id) > 20
ORDER BY COUNT(o.id);
-- Which account has the most orders?
SELECT a.name, COUNT(*) num_orders
FROM accounts a
JOIN orders o
ON a.id = o.account_id
GROUP by a.name
ORDER BY COUNT(o.id) DESC;
-- Which accounts spent more than 30,000 usd total across all orders?
SELECT a.id, a.name, SUM(o.total_amt_usd) total_spent
FROM accounts a
JOIN orders o
ON a.id = o.account_id
GROUP BY a.id, a.name
HAVING SUM(o.total_amt_usd) > 30000
ORDER BY total_spent;
-- Which accounts spent less than 1,000 usd total across all orders?
SELECT a.id, a.name, SUM(o.total_amt_usd) total_spent
FROM accounts a
JOIN orders o
ON a.id = o.account_id
GROUP BY a.id, a.name
HAVING SUM(o.total_amt_usd) < 1000
ORDER BY total_spent;
-- Which account has spent the most with us?
SELECT a.id, a.name, SUM(o.total_amt_usd) total_spent
FROM accounts a
JOIN orders o
ON a.id = o.account_id
GROUP BY a.id, a.name
ORDER BY total_spent DESC
LIMIT 1;
-- Which account has spent the least with us?
SELECT a.id, a.name, SUM(o.total_amt_usd) total_spent
FROM accounts a
JOIN orders o
ON a.id = o.account_id
GROUP BY a.id, a.name
ORDER BY total_spent
LIMIT 1;
-- Which accounts used facebook as a channel to contact customers more than 6 times?
SELECT w.channel, a.id, COUNT(a.id)
FROM web_events w
JOIN accounts a
ON w.account_id = a.id
GROUP BY w.channel, a.id
HAVING w.channel LIKE 'facebook' AND COUNT(a.id) > 6
ORDER BY COUNT(a.id)
-- Which account used facebook most as a channel?
SELECT w.channel,a.name, a.id, COUNT(a.id)
FROM web_events w
JOIN accounts a
ON w.account_id = a.id
GROUP BY w.channel, a.id, a.name
HAVING w.channel LIKE 'facebook'
ORDER BY COUNT(a.id) DESC
LIMIT 1;
-- Which channel was most frequently used by most accounts?
SELECT w.channel,a.name, a.id, COUNT(a.id)
FROM web_events w
JOIN accounts a
ON w.account_id = a.id
GROUP BY w.channel, a.id, a.name
ORDER BY COUNT(a.id) DESC
-- Which channel was most frequently used by most accounts?