-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_as_campground.sql
148 lines (141 loc) · 3.48 KB
/
04_as_campground.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
/*
Goal: learn how to rename a column or table with AS
The AS keyword sets an alias for the column in the query results
The AS keyword also sets an alias for the table in the query statement
The AS keyword can be used implicitly or explicitly
We have seen this query before - notice the new column name
*/
SELECT
COUNT(1) AS num_campgrounds
FROM
campground;
-- result
-- +-----------------+
-- | num_campgrounds |
-- +-----------------+
-- | 638 |
-- +-----------------+
/*
The AS is not required - you can use a space and a string like this
*/
SELECT
COUNT(1) num_campgrounds
FROM
campground;
-- result
-- +-----------------+
-- | num_campgrounds |
-- +-----------------+
-- | 638 |
-- +-----------------+
/*
We can also use the AS keyword to rename a table in the query
By default you can refer to column using the full table name
*/
SELECT
campground.name
FROM
campground
LIMIT 1;
-- result
-- +----------------------+
-- | name |
-- +----------------------+
-- | 277 North Campground |
-- +----------------------+
/*
With one table this is not required
Once there are multiple tables in the query
We need to specify the table name for columns with the same name in both tables
Otherwise the database will not know which column to return
The following query is like a nested loop between the campground and park tables
Here is some python pseudo code to illustrate the query:
result = []
for (campground in campgrounds) {
for (park in parks) {
result.append((campground.name, park.name))
}
}
return result
You usually would not want to do this
Since the performance of the nested loop is O(n^2)
For a better approach see the JOIN exercise
*/
SELECT
campground.name,
park.name
FROM
campground,
park
LIMIT 1;
-- result
-- +----------------------+----------------------------+
-- | name | name |
-- +----------------------+----------------------------+
-- | 277 North Campground | Abraham Lincoln Birthplace |
-- +----------------------+----------------------------+
/*
For now it is important to know that you can rename a table in the sql query with AS
*/
SELECT
c.name,
p.name
FROM
campground AS c,
park AS p
LIMIT 1;
-- result
-- +----------------------+----------------------------+
-- | name | name |
-- +----------------------+----------------------------+
-- | 277 North Campground | Abraham Lincoln Birthplace |
-- +----------------------+----------------------------+
/*
The AS keyword is not required
We can also rename tables with a space and a string
This is the same as the last query
*/
SELECT
c.name,
p.name
FROM
campground c,
park p
LIMIT 1;
-- result
-- +----------------------+----------------------------+
-- | name | name |
-- +----------------------+----------------------------+
-- | 277 North Campground | Abraham Lincoln Birthplace |
-- +----------------------+----------------------------+
/*
Everything else is identical about the query
For example we can count the number of campgrounds that do not allow RVs
*/
SELECT
COUNT(1) no_rv
FROM
campground
WHERE
is_rv_allowed = 0;
-- result
-- +-------+
-- | no_rv |
-- +-------+
-- | 340 |
-- +-------+
/*
And we can count the number of campgrounds that allow RVs
*/
SELECT
COUNT(1) yes_rv
FROM
campground
WHERE
is_rv_allowed = 1;
-- result
-- +--------+
-- | yes_rv |
-- +--------+
-- | 298 |
-- +--------+