Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 52cacf4

Browse files
committed
Improve documentation of JOIN syntax. Explain NATURAL as an alternative
to ON and USING for specifying the join condition, not as an independent kind of join semantics.
1 parent 5c90733 commit 52cacf4

File tree

2 files changed

+138
-132
lines changed

2 files changed

+138
-132
lines changed

doc/src/sgml/queries.sgml

+67-41
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/queries.sgml,v 1.4 2001/02/13 21:13:11 petere Exp $ -->
1+
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/queries.sgml,v 1.5 2001/02/15 04:10:54 tgl Exp $ -->
22

33
<chapter id="queries">
44
<title>Queries</title>
@@ -105,7 +105,7 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
105105
<para>
106106
A joined table is a table derived from two other (real or
107107
derived) tables according to the rules of the particular join
108-
type. INNER, OUTER, NATURAL, and CROSS JOIN are supported.
108+
type. INNER, OUTER, and CROSS JOIN are supported.
109109
</para>
110110

111111
<variablelist>
@@ -122,10 +122,10 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
122122
<para>
123123
For each combination of rows from
124124
<replaceable>T1</replaceable> and
125-
<replaceable>T2</replaceable> the derived table will contain a
125+
<replaceable>T2</replaceable>, the derived table will contain a
126126
row consisting of all columns in <replaceable>T1</replaceable>
127127
followed by all columns in <replaceable>T2</replaceable>. If
128-
the tables have have N and M rows respectively, the joined
128+
the tables have N and M rows respectively, the joined
129129
table will have N * M rows. A cross join is equivalent to an
130130
<literal>INNER JOIN ON TRUE</literal>.
131131
</para>
@@ -148,32 +148,55 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
148148
<synopsis>
149149
<replaceable>T1</replaceable> { <optional>INNER</optional> | { LEFT | RIGHT | FULL } <optional>OUTER</optional> } JOIN <replaceable>T2</replaceable> ON <replaceable>boolean expression</replaceable>
150150
<replaceable>T1</replaceable> { <optional>INNER</optional> | { LEFT | RIGHT | FULL } <optional>OUTER</optional> } JOIN <replaceable>T2</replaceable> USING ( <replaceable>join column list</replaceable> )
151+
<replaceable>T1</replaceable> NATURAL { <optional>INNER</optional> | { LEFT | RIGHT | FULL } <optional>OUTER</optional> } JOIN <replaceable>T2</replaceable>
151152
</synopsis>
152153

153154
<para>
154155
The words <token>INNER</token> and <token>OUTER</token> are
155156
optional for all JOINs. <token>INNER</token> is the default;
156157
<token>LEFT</token>, <token>RIGHT</token>, and
157-
<token>FULL</token> are for OUTER JOINs only.
158+
<token>FULL</token> imply an OUTER JOIN.
158159
</para>
159160

160161
<para>
161162
The <firstterm>join condition</firstterm> is specified in the
162-
ON or USING clause. (The meaning of the join condition
163-
depends on the particular join type; see below.) The ON
164-
clause takes a Boolean value expression of the same kind as is
165-
used in a WHERE clause. The USING clause takes a
163+
ON or USING clause, or implicitly by the word NATURAL. The join
164+
condition determines which rows from the two source tables are
165+
considered to <quote>match</quote>, as explained in detail below.
166+
</para>
167+
168+
<para>
169+
The ON clause is the most general kind of join condition: it takes a
170+
Boolean value expression of the same kind as is used in a WHERE
171+
clause. A pair of rows from T1 and T2 match if the ON expression
172+
evaluates to TRUE for them.
173+
</para>
174+
175+
<para>
176+
USING is a shorthand notation: it takes a
166177
comma-separated list of column names, which the joined tables
167-
must have in common, and joins the tables on the equality of
168-
those columns as a set, resulting in a joined table having one
169-
column for each common column listed and all of the other
170-
columns from both tables. Thus, <literal>USING (a, b,
171-
c)</literal> is equivalent to <literal>ON (t1.a = t2.a AND
172-
t1.b = t2.b AND t1.c = t2.c)</literal> with the exception that
178+
must have in common, and forms a join condition specifying equality
179+
of each of these pairs of columns. Furthermore, the output of
180+
a JOIN USING has one column for each of the equated pairs of
181+
input columns, followed by all of the other columns from each table.
182+
Thus, <literal>USING (a, b, c)</literal> is equivalent to
183+
<literal>ON (t1.a = t2.a AND t1.b = t2.b AND t1.c = t2.c)</literal>
184+
with the exception that
173185
if ON is used there will be two columns a, b, and c in the
174186
result, whereas with USING there will be only one of each.
175187
</para>
176188

189+
<para>
190+
Finally, NATURAL is a shorthand form of USING: it forms a USING
191+
list consisting of exactly those column names that appear in both
192+
input tables. As with USING, these columns appear only once in
193+
the output table.
194+
</para>
195+
196+
<para>
197+
The possible types of qualified JOIN are:
198+
</para>
199+
177200
<variablelist>
178201
<varlistentry>
179202
<term>INNER JOIN</term>
@@ -205,7 +228,10 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
205228

206229
<listitem>
207230
<para>
208-
This is the converse of a left join: the result table will
231+
First, an INNER JOIN is performed. Then, for each row in T2
232+
that does not satisfy the join condition with any row in
233+
T1, a joined row is returned with NULL values in columns of
234+
T1. This is the converse of a left join: the result table will
209235
unconditionally have a row for each row in T2.
210236
</para>
211237
</listitem>
@@ -228,22 +254,6 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
228254
</variablelist>
229255
</listitem>
230256
</varlistentry>
231-
232-
<varlistentry>
233-
<term>NATURAL JOIN</term>
234-
235-
<listitem>
236-
<synopsis>
237-
<replaceable>T1</replaceable> NATURAL { <optional>INNER</optional> | { LEFT | RIGHT | FULL } <optional>OUTER</optional> JOIN <replaceable>T2</replaceable>
238-
</synopsis>
239-
<para>
240-
A natural join creates a joined table where every pair of matching
241-
column names between the two tables are merged into one column. The
242-
result is the same as a qualified join with a USING clause that lists
243-
all the common column names of the two tables.
244-
</para>
245-
</listitem>
246-
</varlistentry>
247257
</variablelist>
248258

249259
<para>
@@ -270,8 +280,9 @@ FROM (SELECT * FROM table1) AS alias_name
270280

271281
<para>
272282
This example is equivalent to <literal>FROM table1 AS
273-
alias_name</literal>. Many subqueries can be written as table
274-
joins instead.
283+
alias_name</literal>. More interesting cases, which can't be
284+
reduced to a plain join, arise when the subquery involves grouping
285+
or aggregation.
275286
</para>
276287
</sect3>
277288

@@ -331,13 +342,28 @@ FROM <replaceable>table_reference</replaceable> <replaceable>alias</replaceable>
331342
<synopsis>
332343
FROM <replaceable>table_reference</replaceable> <optional>AS</optional> <replaceable>alias</replaceable> ( <replaceable>column1</replaceable> <optional>, <replaceable>column2</replaceable> <optional>, ...</optional></optional> )
333344
</synopsis>
334-
In addition to renaming the table as described above, the columns
345+
In this form,
346+
in addition to renaming the table as described above, the columns
335347
of the table are also given temporary names for use by the surrounding
336348
query. If fewer column
337349
aliases are specified than the actual table has columns, the remaining
338350
columns are not renamed. This syntax is especially useful for
339351
self-joins or subqueries.
340352
</para>
353+
354+
<para>
355+
When an alias is applied to the output of a JOIN clause, using any of
356+
these forms, the alias hides the original names within the JOIN.
357+
For example,
358+
<programlisting>
359+
SELECT a.* FROM my_table AS a JOIN your_table AS b ON ...
360+
</programlisting>
361+
is valid SQL, but
362+
<programlisting>
363+
SELECT a.* FROM (my_table AS a JOIN your_table AS b ON ...) AS c
364+
</programlisting>
365+
is not valid: the table alias A is not visible outside the alias C.
366+
</para>
341367
</sect3>
342368

343369
<sect3 id="queries-table-expression-examples">
@@ -442,13 +468,13 @@ FROM FDT WHERE
442468
In the examples above, FDT is the table derived in the FROM
443469
clause. Rows that do not meet the search condition of the where
444470
clause are eliminated from FDT. Notice the use of scalar
445-
subqueries as value expressions (C2 assumed UNIQUE). Just like
471+
subqueries as value expressions. Just like
446472
any other query, the subqueries can employ complex table
447473
expressions. Notice how FDT is referenced in the subqueries.
448474
Qualifying C1 as FDT.C1 is only necessary if C1 is also the name of a
449475
column in the derived input table of the subquery. Qualifying the
450-
column name adds clarity even when it is not needed. The column
451-
naming scope of an outer query extends into its inner queries.
476+
column name adds clarity even when it is not needed. This shows how
477+
the column naming scope of an outer query extends into its inner queries.
452478
</para>
453479
</sect2>
454480

@@ -488,8 +514,8 @@ SELECT <replaceable>select_list</replaceable> FROM ... <optional>WHERE ...</opti
488514
<function>sum(sales)</function> on a table grouped by product code
489515
gives the total sales for each product, not the total sales on all
490516
products. Aggregates computed on the ungrouped columns are
491-
representative of the group, whereas their individual values may
492-
not be.
517+
representative of the group, whereas individual values of an ungrouped
518+
column are not.
493519
</para>
494520

495521
<para>
@@ -583,7 +609,7 @@ SELECT tbl1.a, tbl2.b, tbl1.c FROM ...
583609
<para>
584610
If an arbitrary value expression is used in the select list, it
585611
conceptually adds a new virtual column to the returned table. The
586-
value expression is effectively evaluated once for each retrieved
612+
value expression is evaluated once for each retrieved
587613
row, with the row's values substituted for any column references. But
588614
the expressions in the select list do not have to reference any
589615
columns in the table expression of the FROM clause; they could be

0 commit comments

Comments
 (0)