-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLesson18.sql
More file actions
85 lines (66 loc) · 1.99 KB
/
Copy pathLesson18.sql
File metadata and controls
85 lines (66 loc) · 1.99 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
### DISTINCT
/*
You can think of DISTINCT this way, if you want to group by some columns
but you don’t necessarily want to include any aggregations, you can use
DISTINCT instead.
As an example, let’s see the count of events by channel by account in
the GROUP BY segment.
*/
SELECT account_id,
channel,
COUNT(id) AS events
FROM web_events
GROUP BY account_id, channel
ORDER BY account_id, events DESC
# Notice the row count, 1509
/*
If we get rid of the events column, you can see that the resulting query
returns basically the results with the same row count. And if run that
query again with DISTINCT, you can see that the results are the same.
*/
SELECT DISTINCT account_id,
channel
FROM web_events
ORDER BY account_id
# Notice the row count, 1509
/*
Use DISTINCT to test if there are any accounts associated with more than
one region.
*/
SELECT a.id as "account id", r.id as "region id",
a.name as "account name", r.name as "region name"
FROM accounts a
JOIN sales_reps s
ON s.id = a.sales_rep_id
JOIN region r
ON r.id = s.region_id;
# Notice the row count, 351
SELECT DISTINCT id, name
FROM accounts;
# Notice the row count, 351
/*
The two queries have the same number of resulting rows (351), so we know
that every account is associated with only one region. If each account
was associated with more than one region, the first query should have
returned more rows than the second query.
*/
/*
Have any sales reps worked on more than one account?
*/
SELECT s.id, s.name, COUNT(*) num_accounts
FROM accounts a
JOIN sales_reps s
ON s.id = a.sales_rep_id
GROUP BY s.id, s.name
ORDER BY num_accounts;
# Notice the row count, 50
SELECT DISTINCT id, name
FROM sales_reps;
# Notice the row count, 50
/*
Actually all of the sales reps have worked on more than one account. The
fewest number of accounts any sales rep works on is 3. There are 50
sales reps, and they all have more than one account. Using DISTINCT in
the second query assures that all of the sales reps are accounted for in
the first query.
*/