You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _episodes/06-joins-aliases.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ FROM articles
26
26
JOIN journals
27
27
ON articles.ISSNs = journals.ISSNs;
28
28
~~~
29
-
{: .source}
29
+
{: .sql}
30
30
31
31
`ON` is similar to `WHERE`, it filters things out according to a test condition. We use the `table.colname` format to tell the SQL manager what column in which table we are referring to.
32
32
@@ -38,7 +38,7 @@ FROM articles
38
38
JOIN journals
39
39
USING (ISSNs);
40
40
~~~
41
-
{: .source}
41
+
{: .sql}
42
42
43
43
This figure shows the relations between the tables and helps to visualise joining or linking the tables in the database:
Joins can be combined with sorting, filtering, and aggregation. So, if we wanted the average number of authors for articles on each journal, we can use the following query:
58
58
@@ -63,7 +63,7 @@ JOIN journals
63
63
ON articles.ISSNs = journals.ISSNs
64
64
GROUP BY articles.ISSNs;
65
65
~~~
66
-
{: .source}
66
+
{: .sql}
67
67
68
68
The `ROUND` function allows us to round the `Author_Count` number returned by the `AVG` function by 2 decimal places.
69
69
@@ -92,7 +92,7 @@ ON articles.ISSNs = journals.ISSNs
92
92
JOIN publishers
93
93
ON publishers.id = journals.PublisherId;
94
94
~~~
95
-
{: .source}
95
+
{: .sql}
96
96
97
97
> ## Challenge:
98
98
>
@@ -129,7 +129,7 @@ FROM articles AS ar
129
129
JOIN journals AS jo
130
130
ON ar.ISSNs = jo.ISSNs;
131
131
~~~
132
-
{: .source}
132
+
{: .sql}
133
133
134
134
And column names:
135
135
@@ -139,15 +139,15 @@ FROM articles AS ar
139
139
JOIN journals AS jo
140
140
ON ar.issns = jo.issns;
141
141
~~~
142
-
{: .source}
142
+
{: .sql}
143
143
144
144
The `AS` isn't technically required, so you could do:
145
145
146
146
~~~
147
147
SELECT a.Title t
148
148
FROM articles a;
149
149
~~~
150
-
{: .source}
150
+
{: .sql}
151
151
152
152
But using `AS` is much clearer so it is good style to include it.
0 commit comments