@@ -118,10 +118,12 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
118
118
</synopsis>
119
119
120
120
A table reference can be a table name (possibly schema-qualified),
121
- or a derived table such as a subquery, a table join, or complex
122
- combinations of these. If more than one table reference is listed
123
- in the <literal>FROM</> clause they are cross-joined (see below)
124
- to form the intermediate virtual table that can then be subject to
121
+ or a derived table such as a subquery, a <literal>JOIN</> construct, or
122
+ complex combinations of these. If more than one table reference is
123
+ listed in the <literal>FROM</> clause, the tables are cross-joined
124
+ (that is, the Cartesian product of their rows is formed; see below).
125
+ The result of the <literal>FROM</> list is an intermediate virtual
126
+ table that can then be subject to
125
127
transformations by the <literal>WHERE</>, <literal>GROUP BY</>,
126
128
and <literal>HAVING</> clauses and is finally the result of the
127
129
overall table expression.
@@ -161,6 +163,16 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
161
163
A joined table is a table derived from two other (real or
162
164
derived) tables according to the rules of the particular join
163
165
type. Inner, outer, and cross-joins are available.
166
+ The general syntax of a joined table is
167
+ <synopsis>
168
+ <replaceable>T1</replaceable> <replaceable>join_type</replaceable> <replaceable>T2</replaceable> <optional> <replaceable>join_condition</replaceable> </optional>
169
+ </synopsis>
170
+ Joins of all types can be chained together, or nested: either or
171
+ both <replaceable>T1</replaceable> and
172
+ <replaceable>T2</replaceable> can be joined tables. Parentheses
173
+ can be used around <literal>JOIN</> clauses to control the join
174
+ order. In the absence of parentheses, <literal>JOIN</> clauses
175
+ nest left-to-right.
164
176
</para>
165
177
166
178
<variablelist>
@@ -197,10 +209,28 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
197
209
<para>
198
210
<literal>FROM <replaceable>T1</replaceable> CROSS JOIN
199
211
<replaceable>T2</replaceable></literal> is equivalent to
200
- <literal>FROM <replaceable>T1</replaceable>,
201
- <replaceable>T2</replaceable></literal>. It is also equivalent to
202
212
<literal>FROM <replaceable>T1</replaceable> INNER JOIN
203
213
<replaceable>T2</replaceable> ON TRUE</literal> (see below).
214
+ It is also equivalent to
215
+ <literal>FROM <replaceable>T1</replaceable>,
216
+ <replaceable>T2</replaceable></literal>.
217
+ <note>
218
+ <para>
219
+ This latter equivalence does not hold exactly when more than two
220
+ tables appear, because <literal>JOIN</> binds more tightly than
221
+ comma. For example
222
+ <literal>FROM <replaceable>T1</replaceable> CROSS JOIN
223
+ <replaceable>T2</replaceable> INNER JOIN <replaceable>T3</replaceable>
224
+ ON <replaceable>condition</replaceable></literal>
225
+ is not the same as
226
+ <literal>FROM <replaceable>T1</replaceable>,
227
+ <replaceable>T2</replaceable> INNER JOIN <replaceable>T3</replaceable>
228
+ ON <replaceable>condition</replaceable></literal>
229
+ because the <replaceable>condition</replaceable> can
230
+ reference <replaceable>T1</replaceable> in the first case but not
231
+ the second.
232
+ </para>
233
+ </note>
204
234
</para>
205
235
</listitem>
206
236
</varlistentry>
@@ -240,47 +270,6 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
240
270
<quote>match</quote>, as explained in detail below.
241
271
</para>
242
272
243
- <para>
244
- The <literal>ON</> clause is the most general kind of join
245
- condition: it takes a Boolean value expression of the same
246
- kind as is used in a <literal>WHERE</> clause. A pair of rows
247
- from <replaceable>T1</> and <replaceable>T2</> match if the
248
- <literal>ON</> expression evaluates to true for them.
249
- </para>
250
-
251
- <para>
252
- <literal>USING</> is a shorthand notation: it takes a
253
- comma-separated list of column names, which the joined tables
254
- must have in common, and forms a join condition specifying
255
- equality of each of these pairs of columns. Furthermore, the
256
- output of <literal>JOIN USING</> has one column for each of
257
- the equated pairs of input columns, followed by the
258
- remaining columns from each table. Thus, <literal>USING (a, b,
259
- c)</literal> is equivalent to <literal>ON (t1.a = t2.a AND
260
- t1.b = t2.b AND t1.c = t2.c)</literal> with the exception that
261
- if <literal>ON</> is used there will be two columns
262
- <literal>a</>, <literal>b</>, and <literal>c</> in the result,
263
- whereas with <literal>USING</> there will be only one of each
264
- (and they will appear first if <command>SELECT *</> is used).
265
- </para>
266
-
267
- <para>
268
- <indexterm>
269
- <primary>join</primary>
270
- <secondary>natural</secondary>
271
- </indexterm>
272
- <indexterm>
273
- <primary>natural join</primary>
274
- </indexterm>
275
- Finally, <literal>NATURAL</> is a shorthand form of
276
- <literal>USING</>: it forms a <literal>USING</> list
277
- consisting of all column names that appear in both
278
- input tables. As with <literal>USING</>, these columns appear
279
- only once in the output table. If there are no common
280
- columns, <literal>NATURAL</literal> behaves like
281
- <literal>CROSS JOIN</literal>.
282
- </para>
283
-
284
273
<para>
285
274
The possible types of qualified join are:
286
275
@@ -358,19 +347,70 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
358
347
</varlistentry>
359
348
</variablelist>
360
349
</para>
350
+
351
+ <para>
352
+ The <literal>ON</> clause is the most general kind of join
353
+ condition: it takes a Boolean value expression of the same
354
+ kind as is used in a <literal>WHERE</> clause. A pair of rows
355
+ from <replaceable>T1</> and <replaceable>T2</> match if the
356
+ <literal>ON</> expression evaluates to true.
357
+ </para>
358
+
359
+ <para>
360
+ The <literal>USING</> clause is a shorthand that allows you to take
361
+ advantage of the specific situation where both sides of the join use
362
+ the same name for the joining column(s). It takes a
363
+ comma-separated list of the shared column names
364
+ and forms a join condition that includes an equality comparison
365
+ for each one. For example, joining <replaceable>T1</>
366
+ and <replaceable>T2</> with <literal>USING (a, b)</> produces
367
+ the join condition <literal>ON <replaceable>T1</>.a
368
+ = <replaceable>T2</>.a AND <replaceable>T1</>.b
369
+ = <replaceable>T2</>.b</literal>.
370
+ </para>
371
+
372
+ <para>
373
+ Furthermore, the output of <literal>JOIN USING</> suppresses
374
+ redundant columns: there is no need to print both of the matched
375
+ columns, since they must have equal values. While <literal>JOIN
376
+ ON</> produces all columns from <replaceable>T1</> followed by all
377
+ columns from <replaceable>T2</>, <literal>JOIN USING</> produces one
378
+ output column for each of the listed column pairs (in the listed
379
+ order), followed by any remaining columns from <replaceable>T1</>,
380
+ followed by any remaining columns from <replaceable>T2</>.
381
+ </para>
382
+
383
+ <para>
384
+ <indexterm>
385
+ <primary>join</primary>
386
+ <secondary>natural</secondary>
387
+ </indexterm>
388
+ <indexterm>
389
+ <primary>natural join</primary>
390
+ </indexterm>
391
+ Finally, <literal>NATURAL</> is a shorthand form of
392
+ <literal>USING</>: it forms a <literal>USING</> list
393
+ consisting of all column names that appear in both
394
+ input tables. As with <literal>USING</>, these columns appear
395
+ only once in the output table. If there are no common
396
+ column names, <literal>NATURAL</literal> behaves like
397
+ <literal>CROSS JOIN</literal>.
398
+ </para>
399
+
400
+ <note>
401
+ <para>
402
+ <literal>USING</literal> is reasonably safe from column changes
403
+ in the joined relations since only the listed columns
404
+ are combined. <literal>NATURAL</> is considerably more risky since
405
+ any schema changes to either relation that cause a new matching
406
+ column name to be present will cause the join to combine that new
407
+ column as well.
408
+ </para>
409
+ </note>
361
410
</listitem>
362
411
</varlistentry>
363
412
</variablelist>
364
413
365
- <para>
366
- Joins of all types can be chained together or nested: either or
367
- both <replaceable>T1</replaceable> and
368
- <replaceable>T2</replaceable> can be joined tables. Parentheses
369
- can be used around <literal>JOIN</> clauses to control the join
370
- order. In the absence of parentheses, <literal>JOIN</> clauses
371
- nest left-to-right.
372
- </para>
373
-
374
414
<para>
375
415
To put this together, assume we have tables <literal>t1</literal>:
376
416
<programlisting>
@@ -487,6 +527,8 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
487
527
clause is processed <emphasis>before</> the join, while
488
528
a restriction placed in the <literal>WHERE</> clause is processed
489
529
<emphasis>after</> the join.
530
+ That does not matter with inner joins, but it matters a lot with outer
531
+ joins.
490
532
</para>
491
533
</sect3>
492
534
0 commit comments