From 181cc0906b9130f0acb37131a50c06a601ec115c Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Wed, 31 Aug 2022 21:46:14 -0400 Subject: [PATCH] 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 --- doc/src/sgml/ref/select.sgml | 46 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index aa823d2ce7..3af337a65c 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -59,7 +59,9 @@ SELECT [ ALL | DISTINCT [ ON ( expressionfunction_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] ) [ LATERAL ] ROWS FROM( function_name ( [ argument [, ...] ] ) [ AS ( column_definition [, ...] ) ] [, ...] ) [ WITH ORDINALITY ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ] - from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ] + from_item join_type from_item { ON join_condition | USING ( join_column [, ...] ) } + from_item NATURAL join_type from_item + from_item CROSS JOIN from_item and grouping_element can be one of: @@ -554,19 +556,15 @@ TABLE [ ONLY ] table_name [ * ] FULL [ OUTER ] JOIN - - CROSS JOIN - For the INNER and OUTER join types, a join condition must be specified, namely exactly one of - NATURAL, ON join_condition, or + ON join_condition, USING (join_column [, ...]). - See below for the meaning. For CROSS JOIN, - none of these clauses can appear. + class="parameter">join_column [, ...]), + or NATURAL. See below for the meaning. @@ -577,17 +575,9 @@ TABLE [ ONLY ] table_name [ * ] In the absence of parentheses, JOINs nest left-to-right. In any case JOIN binds more tightly than the commas separating FROM-list items. - - - CROSS JOIN and INNER JOIN - produce a simple Cartesian product, the same result as you get from - listing the two tables at the top level of FROM, - but restricted by the join condition (if any). - CROSS JOIN is equivalent to INNER JOIN ON - (TRUE), that is, no rows are removed by qualification. - These join types are just a notational convenience, since they - do nothing you couldn't do with plain FROM and - WHERE. + All the JOIN options are just a notational + convenience, since they do nothing you couldn't do with plain + FROM and WHERE. LEFT OUTER JOIN returns all rows in the qualified @@ -656,6 +646,19 @@ TABLE [ ONLY ] table_name [ * ] + + CROSS JOIN + + + CROSS JOIN is equivalent to INNER JOIN ON + (TRUE), that is, no rows are removed by qualification. + They produce a simple Cartesian product, the same result as you get from + listing the two tables at the top level of FROM, + but restricted by the join condition (if any). + + + + LATERAL @@ -1693,8 +1696,7 @@ SELECT * FROM name SELECT f.title, f.did, d.name, f.date_prod, f.kind - FROM distributors d, films f - WHERE f.did = d.did + FROM distributors d JOIN films f USING (did); title | did | name | date_prod | kind -------------------+-----+--------------+------------+----------