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
@@ -105,7 +105,7 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
105
105
<para>
106
106
A joined table is a table derived from two other (real or
107
107
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.
109
109
</para>
110
110
111
111
<variablelist>
@@ -122,10 +122,10 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
122
122
<para>
123
123
For each combination of rows from
124
124
<replaceable>T1</replaceable> and
125
-
<replaceable>T2</replaceable> the derived table will contain a
125
+
<replaceable>T2</replaceable>, the derived table will contain a
126
126
row consisting of all columns in <replaceable>T1</replaceable>
127
127
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
129
129
table will have N * M rows. A cross join is equivalent to an
130
130
<literal>INNER JOIN ON TRUE</literal>.
131
131
</para>
@@ -148,32 +148,55 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
148
148
<synopsis>
149
149
<replaceable>T1</replaceable> { <optional>INNER</optional> | { LEFT | RIGHT | FULL } <optional>OUTER</optional> } JOIN <replaceable>T2</replaceable> ON <replaceable>boolean expression</replaceable>
150
150
<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>
151
152
</synopsis>
152
153
153
154
<para>
154
155
The words <token>INNER</token> and <token>OUTER</token> are
155
156
optional for all JOINs. <token>INNER</token> is the default;
156
157
<token>LEFT</token>, <token>RIGHT</token>, and
157
-
<token>FULL</token> are for OUTER JOINs only.
158
+
<token>FULL</token> imply an OUTER JOIN.
158
159
</para>
159
160
160
161
<para>
161
162
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
166
177
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
173
185
if ON is used there will be two columns a, b, and c in the
174
186
result, whereas with USING there will be only one of each.
175
187
</para>
176
188
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
+
177
200
<variablelist>
178
201
<varlistentry>
179
202
<term>INNER JOIN</term>
@@ -205,7 +228,10 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
205
228
206
229
<listitem>
207
230
<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
209
235
unconditionally have a row for each row in T2.
210
236
</para>
211
237
</listitem>
@@ -228,22 +254,6 @@ FROM <replaceable>table_reference</replaceable> <optional>, <replaceable>table_r
228
254
</variablelist>
229
255
</listitem>
230
256
</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>
247
257
</variablelist>
248
258
249
259
<para>
@@ -270,8 +280,9 @@ FROM (SELECT * FROM table1) AS alias_name
270
280
271
281
<para>
272
282
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.
275
286
</para>
276
287
</sect3>
277
288
@@ -331,13 +342,28 @@ FROM <replaceable>table_reference</replaceable> <replaceable>alias</replaceable>
0 commit comments