Skip to content

Commit 032f3b7

Browse files
committed
doc: improve USING and NATURAL JOIN descriptions
Patch by David G Johnston
1 parent 98aed6c commit 032f3b7

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

doc/src/sgml/queries.sgml

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -245,25 +245,43 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
245245
condition: it takes a Boolean value expression of the same
246246
kind as is used in a <literal>WHERE</> clause. A pair of rows
247247
from <replaceable>T1</> and <replaceable>T2</> match if the
248-
<literal>ON</> expression evaluates to true for them.
248+
<literal>ON</> expression evaluates to true.
249249
</para>
250250

251251
<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).
252+
The <literal>USING</> clause allows you to take advantage of
253+
the specific situation where both sides of the join use the
254+
same name for the joining columns. It takes a
255+
comma-separated list of the shared column names
256+
and forms a join using the equals operator. Furthermore, the
257+
output of <literal>JOIN USING</> has one column for each of the
258+
listed columns, followed by the remaining columns from each table.
265259
</para>
266260

261+
<para>The output column difference between <literal>ON</> and
262+
<literal>USING</> when invoking <literal>SELECT *</> is:</para>
263+
<itemizedlist>
264+
<listitem>
265+
<para>
266+
<literal>ON</> - all columns from <replaceable>T1</> followed
267+
by all columns from <replaceable>T2</>
268+
</para>
269+
</listitem>
270+
<listitem>
271+
<para>
272+
<literal>USING</> - all join columns, one copy each
273+
and in the listed order, followed by non-join columns
274+
in <replaceable>T1</> followed by non-join columns in
275+
<replaceable>T2</>
276+
</para>
277+
</listitem>
278+
<listitem>
279+
<para>
280+
Examples provided below
281+
</para>
282+
</listitem>
283+
</itemizedlist>
284+
267285
<para>
268286
<indexterm>
269287
<primary>join</primary>
@@ -281,6 +299,17 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
281299
<literal>CROSS JOIN</literal>.
282300
</para>
283301

302+
<note>
303+
<para>
304+
<literal>USING</literal> is reasonably safe from column changes
305+
in the joined relations since only the specific columns mentioned
306+
are considered. <literal>NATURAL</> is considerably more problematic
307+
if you are referring to relations only by name (views and tables)
308+
since any schema changes to either relation that cause a new matching
309+
column name to be present will cause the join to consider that new column.
310+
</para>
311+
</note>
312+
284313
<para>
285314
The possible types of qualified join are:
286315

0 commit comments

Comments
 (0)