@@ -118,10 +118,12 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
118118</synopsis>
119119
120120 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
125127 transformations by the <literal>WHERE</>, <literal>GROUP BY</>,
126128 and <literal>HAVING</> clauses and is finally the result of the
127129 overall table expression.
@@ -161,6 +163,16 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
161163 A joined table is a table derived from two other (real or
162164 derived) tables according to the rules of the particular join
163165 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.
164176 </para>
165177
166178 <variablelist>
@@ -197,10 +209,28 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
197209 <para>
198210 <literal>FROM <replaceable>T1</replaceable> CROSS JOIN
199211 <replaceable>T2</replaceable></literal> is equivalent to
200- <literal>FROM <replaceable>T1</replaceable>,
201- <replaceable>T2</replaceable></literal>. It is also equivalent to
202212 <literal>FROM <replaceable>T1</replaceable> INNER JOIN
203213 <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>
204234 </para>
205235 </listitem>
206236 </varlistentry>
@@ -240,45 +270,6 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
240270 <quote>match</quote>, as explained in detail below.
241271 </para>
242272
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.
280- </para>
281-
282273 <para>
283274 The possible types of qualified join are:
284275
@@ -356,19 +347,70 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
356347 </varlistentry>
357348 </variablelist>
358349 </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>
359410 </listitem>
360411 </varlistentry>
361412 </variablelist>
362413
363- <para>
364- Joins of all types can be chained together or nested: either or
365- both <replaceable>T1</replaceable> and
366- <replaceable>T2</replaceable> can be joined tables. Parentheses
367- can be used around <literal>JOIN</> clauses to control the join
368- order. In the absence of parentheses, <literal>JOIN</> clauses
369- nest left-to-right.
370- </para>
371-
372414 <para>
373415 To put this together, assume we have tables <literal>t1</literal>:
374416<programlisting>
@@ -482,9 +524,11 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
482524(1 row)
483525</screen>
484526 This is because a restriction placed in the <literal>ON</>
485- clause is processed <emphasis>before</> the join, while
527+ clause is processed <emphasis>before</> the join, while
486528 a restriction placed in the <literal>WHERE</> clause is processed
487529 <emphasis>after</> the join.
530+ That does not matter with inner joins, but it matters a lot with outer
531+ joins.
488532 </para>
489533 </sect3>
490534