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

Commit 3e337b5

Browse files
committed
doc: split out the NATURAL/CROSS JOIN in SELECT syntax
This allows the syntax to be more accurate about what clauses are supported. Also switch an example query to use the ANSI join syntax. Reported-by: Joel Jacobson Discussion: https://postgr.es/m/67b71d3e-0c22-44df-a223-351f14418319@www.fastmail.com Backpatch-through: 11
1 parent 1703726 commit 3e337b5

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

doc/src/sgml/ref/select.sgml

+24-22
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="parameter">expression</replac
5959
[ LATERAL ] <replaceable class="parameter">function_name</replaceable> ( [ <replaceable class="parameter">argument</replaceable> [, ...] ] ) AS ( <replaceable class="parameter">column_definition</replaceable> [, ...] )
6060
[ LATERAL ] ROWS FROM( <replaceable class="parameter">function_name</replaceable> ( [ <replaceable class="parameter">argument</replaceable> [, ...] ] ) [ AS ( <replaceable class="parameter">column_definition</replaceable> [, ...] ) ] [, ...] )
6161
[ WITH ORDINALITY ] [ [ AS ] <replaceable class="parameter">alias</replaceable> [ ( <replaceable class="parameter">column_alias</replaceable> [, ...] ) ] ]
62-
<replaceable class="parameter">from_item</replaceable> [ NATURAL ] <replaceable class="parameter">join_type</replaceable> <replaceable class="parameter">from_item</replaceable> [ ON <replaceable class="parameter">join_condition</replaceable> | USING ( <replaceable class="parameter">join_column</replaceable> [, ...] ) [ AS <replaceable class="parameter">join_using_alias</replaceable> ] ]
62+
<replaceable class="parameter">from_item</replaceable> <replaceable class="parameter">join_type</replaceable> <replaceable class="parameter">from_item</replaceable> { ON <replaceable class="parameter">join_condition</replaceable> | USING ( <replaceable class="parameter">join_column</replaceable> [, ...] ) [ AS <replaceable class="parameter">join_using_alias</replaceable> ] }
63+
<replaceable class="parameter">from_item</replaceable> NATURAL <replaceable class="parameter">join_type</replaceable> <replaceable class="parameter">from_item</replaceable>
64+
<replaceable class="parameter">from_item</replaceable> CROSS JOIN <replaceable class="parameter">from_item</replaceable>
6365

6466
<phrase>and <replaceable class="parameter">grouping_element</replaceable> can be one of:</phrase>
6567

@@ -600,19 +602,15 @@ TABLE [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ]
600602
<listitem>
601603
<para><literal>FULL [ OUTER ] JOIN</literal></para>
602604
</listitem>
603-
<listitem>
604-
<para><literal>CROSS JOIN</literal></para>
605-
</listitem>
606605
</itemizedlist>
607606

608607
For the <literal>INNER</literal> and <literal>OUTER</literal> join types, a
609608
join condition must be specified, namely exactly one of
610-
<literal>NATURAL</literal>, <literal>ON <replaceable
611-
class="parameter">join_condition</replaceable></literal>, or
609+
<literal>ON <replaceable
610+
class="parameter">join_condition</replaceable></literal>,
612611
<literal>USING (<replaceable
613-
class="parameter">join_column</replaceable> [, ...])</literal>.
614-
See below for the meaning. For <literal>CROSS JOIN</literal>,
615-
none of these clauses can appear.
612+
class="parameter">join_column</replaceable> [, ...])</literal>,
613+
or <literal>NATURAL</literal>. See below for the meaning.
616614
</para>
617615

618616
<para>
@@ -623,17 +621,9 @@ TABLE [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ]
623621
In the absence of parentheses, <literal>JOIN</literal>s nest
624622
left-to-right. In any case <literal>JOIN</literal> binds more
625623
tightly than the commas separating <literal>FROM</literal>-list items.
626-
</para>
627-
628-
<para><literal>CROSS JOIN</literal> and <literal>INNER JOIN</literal>
629-
produce a simple Cartesian product, the same result as you get from
630-
listing the two tables at the top level of <literal>FROM</literal>,
631-
but restricted by the join condition (if any).
632-
<literal>CROSS JOIN</literal> is equivalent to <literal>INNER JOIN ON
633-
(TRUE)</literal>, that is, no rows are removed by qualification.
634-
These join types are just a notational convenience, since they
635-
do nothing you couldn't do with plain <literal>FROM</literal> and
636-
<literal>WHERE</literal>.
624+
All the <literal>JOIN</literal> options are just a notational
625+
convenience, since they do nothing you couldn't do with plain
626+
<literal>FROM</literal> and <literal>WHERE</literal>.
637627
</para>
638628

639629
<para><literal>LEFT OUTER JOIN</literal> returns all rows in the qualified
@@ -714,6 +704,19 @@ TABLE [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ]
714704
</listitem>
715705
</varlistentry>
716706

707+
<varlistentry>
708+
<term><literal>CROSS JOIN</literal></term>
709+
<listitem>
710+
<para>
711+
<literal>CROSS JOIN</literal> is equivalent to <literal>INNER JOIN ON
712+
(TRUE)</literal>, that is, no rows are removed by qualification.
713+
They produce a simple Cartesian product, the same result as you get from
714+
listing the two tables at the top level of <literal>FROM</literal>,
715+
but restricted by the join condition (if any).
716+
</para>
717+
</listitem>
718+
</varlistentry>
719+
717720
<varlistentry>
718721
<term><literal>LATERAL</literal></term>
719722
<listitem>
@@ -1754,8 +1757,7 @@ SELECT * FROM <replaceable class="parameter">name</replaceable>
17541757

17551758
<programlisting>
17561759
SELECT f.title, f.did, d.name, f.date_prod, f.kind
1757-
FROM distributors d, films f
1758-
WHERE f.did = d.did
1760+
FROM distributors d JOIN films f USING (did);
17591761

17601762
title | did | name | date_prod | kind
17611763
-------------------+-----+--------------+------------+----------

0 commit comments

Comments
 (0)