diff --git a/doc/src/sgml/queries.sgml b/doc/src/sgml/queries.sgml index 948aa4849b..28b356da4d 100644 --- a/doc/src/sgml/queries.sgml +++ b/doc/src/sgml/queries.sgml @@ -1,4 +1,4 @@ - + Queries @@ -105,7 +105,7 @@ FROM table_reference , table_r A joined table is a table derived from two other (real or derived) tables according to the rules of the particular join - type. INNER, OUTER, NATURAL, and CROSS JOIN are supported. + type. INNER, OUTER, and CROSS JOIN are supported. @@ -122,10 +122,10 @@ FROM table_reference , table_r For each combination of rows from T1 and - T2 the derived table will contain a + T2, the derived table will contain a row consisting of all columns in T1 followed by all columns in T2. If - the tables have have N and M rows respectively, the joined + the tables have N and M rows respectively, the joined table will have N * M rows. A cross join is equivalent to an INNER JOIN ON TRUE. @@ -148,32 +148,55 @@ FROM table_reference , table_r T1 { INNER | { LEFT | RIGHT | FULL } OUTER } JOIN T2 ON boolean expression T1 { INNER | { LEFT | RIGHT | FULL } OUTER } JOIN T2 USING ( join column list ) +T1 NATURAL { INNER | { LEFT | RIGHT | FULL } OUTER } JOIN T2 The words INNER and OUTER are optional for all JOINs. INNER is the default; LEFT, RIGHT, and - FULL are for OUTER JOINs only. + FULL imply an OUTER JOIN. The join condition is specified in the - ON or USING clause. (The meaning of the join condition - depends on the particular join type; see below.) The ON - clause takes a Boolean value expression of the same kind as is - used in a WHERE clause. The USING clause takes a + ON or USING clause, or implicitly by the word NATURAL. The join + condition determines which rows from the two source tables are + considered to match, as explained in detail below. + + + + The ON clause is the most general kind of join condition: it takes a + Boolean value expression of the same kind as is used in a WHERE + clause. A pair of rows from T1 and T2 match if the ON expression + evaluates to TRUE for them. + + + + USING is a shorthand notation: it takes a comma-separated list of column names, which the joined tables - must have in common, and joins the tables on the equality of - those columns as a set, resulting in a joined table having one - column for each common column listed and all of the other - columns from both tables. Thus, USING (a, b, - c) is equivalent to ON (t1.a = t2.a AND - t1.b = t2.b AND t1.c = t2.c) with the exception that + must have in common, and forms a join condition specifying equality + of each of these pairs of columns. Furthermore, the output of + a JOIN USING has one column for each of the equated pairs of + input columns, followed by all of the other columns from each table. + Thus, USING (a, b, c) is equivalent to + ON (t1.a = t2.a AND t1.b = t2.b AND t1.c = t2.c) + with the exception that if ON is used there will be two columns a, b, and c in the result, whereas with USING there will be only one of each. + + Finally, NATURAL is a shorthand form of USING: it forms a USING + list consisting of exactly those column names that appear in both + input tables. As with USING, these columns appear only once in + the output table. + + + + The possible types of qualified JOIN are: + + INNER JOIN @@ -205,7 +228,10 @@ FROM table_reference , table_r - This is the converse of a left join: the result table will + First, an INNER JOIN is performed. Then, for each row in T2 + that does not satisfy the join condition with any row in + T1, a joined row is returned with NULL values in columns of + T1. This is the converse of a left join: the result table will unconditionally have a row for each row in T2. @@ -228,22 +254,6 @@ FROM table_reference , table_r - - - NATURAL JOIN - - - -T1 NATURAL { INNER | { LEFT | RIGHT | FULL } OUTER JOIN T2 - - - A natural join creates a joined table where every pair of matching - column names between the two tables are merged into one column. The - result is the same as a qualified join with a USING clause that lists - all the common column names of the two tables. - - - @@ -270,8 +280,9 @@ FROM (SELECT * FROM table1) AS alias_name This example is equivalent to FROM table1 AS - alias_name. Many subqueries can be written as table - joins instead. + alias_name. More interesting cases, which can't be + reduced to a plain join, arise when the subquery involves grouping + or aggregation. @@ -331,13 +342,28 @@ FROM table_reference alias FROM table_reference AS alias ( column1 , column2 , ... ) - In addition to renaming the table as described above, the columns + In this form, + in addition to renaming the table as described above, the columns of the table are also given temporary names for use by the surrounding query. If fewer column aliases are specified than the actual table has columns, the remaining columns are not renamed. This syntax is especially useful for self-joins or subqueries. + + + When an alias is applied to the output of a JOIN clause, using any of + these forms, the alias hides the original names within the JOIN. + For example, + +SELECT a.* FROM my_table AS a JOIN your_table AS b ON ... + + is valid SQL, but + +SELECT a.* FROM (my_table AS a JOIN your_table AS b ON ...) AS c + + is not valid: the table alias A is not visible outside the alias C. + @@ -442,13 +468,13 @@ FROM FDT WHERE In the examples above, FDT is the table derived in the FROM clause. Rows that do not meet the search condition of the where clause are eliminated from FDT. Notice the use of scalar - subqueries as value expressions (C2 assumed UNIQUE). Just like + subqueries as value expressions. Just like any other query, the subqueries can employ complex table expressions. Notice how FDT is referenced in the subqueries. Qualifying C1 as FDT.C1 is only necessary if C1 is also the name of a column in the derived input table of the subquery. Qualifying the - column name adds clarity even when it is not needed. The column - naming scope of an outer query extends into its inner queries. + column name adds clarity even when it is not needed. This shows how + the column naming scope of an outer query extends into its inner queries. @@ -488,8 +514,8 @@ SELECT select_list FROM ... WHERE ...sum(sales) on a table grouped by product code gives the total sales for each product, not the total sales on all products. Aggregates computed on the ungrouped columns are - representative of the group, whereas their individual values may - not be. + representative of the group, whereas individual values of an ungrouped + column are not. @@ -583,7 +609,7 @@ SELECT tbl1.a, tbl2.b, tbl1.c FROM ... If an arbitrary value expression is used in the select list, it conceptually adds a new virtual column to the returned table. The - value expression is effectively evaluated once for each retrieved + value expression is evaluated once for each retrieved row, with the row's values substituted for any column references. But the expressions in the select list do not have to reference any columns in the table expression of the FROM clause; they could be diff --git a/doc/src/sgml/sql.sgml b/doc/src/sgml/sql.sgml index 80c261fa75..7b21d34b8d 100644 --- a/doc/src/sgml/sql.sgml +++ b/doc/src/sgml/sql.sgml @@ -1,5 +1,5 @@ @@ -1063,15 +1063,11 @@ select sname, pname from supplier - JOINs of all types can be chained together or nested where either or both of - T1 and - T2 may be JOINed tables. - A Qualified JOIN may be JOINed to another table (or JOINed table) - following its join specification, which consists of either an - ON search condition or - USING ( join column list ) clause. - Parenthesis can be used around JOIN clauses to control the order - of JOINs which are otherwise processed left to right. + SQL JOINs come in two main types, CROSS JOINs (unqualified joins) + and qualified JOINs. Qualified joins can be further + subdivided based on the way in which the join condition + is specified (ON, USING, or NATURAL) and the way in which it is + applied (INNER or OUTER join). @@ -1081,18 +1077,17 @@ select sname, pname from supplier T1 - CROSS - JOIN + CROSS JOIN T2 A cross join takes two tables T1 and T2 having N and M rows - respectively, and returns a joined table containing a cross - product, NxM, of joined rows. For each row R1 of T1, each row + respectively, and returns a joined table containing all + N*M possible joined rows. For each row R1 of T1, each row R2 of T2 is joined with R1 to yield a joined table row JR consisting of all fields in R1 and R2. A CROSS JOIN is - essentially an INNER JOIN ON TRUE. + equivalent to an INNER JOIN ON TRUE. @@ -1102,34 +1097,41 @@ select sname, pname from supplier - T1 - - INNER - - - LEFT - RIGHT - FULL - - OUTER + T1 + NATURAL + + INNER + + + LEFT + RIGHT + FULL + + OUTER - JOIN - T2 - - ON search condition - USING ( join column list ) - - ... + JOIN + T2 + + ON search condition + USING ( join column list ) + - Only the qualified JOIN types can use ON or USING clauses. The ON clause - takes a search condition, which is the same - as in a WHERE clause. The USING clause takes a comma-separated list of - column names, which the joined tables must have in common, and joins - the tables on those columns, resulting in a joined table having one - column for each common column and all of the other columns from both tables. + A qualified JOIN must specify its join condition + by providing one (and only one) of NATURAL, ON, or + USING. The ON clause + takes a search condition, + which is the same as in a WHERE clause. The USING + clause takes a comma-separated list of column names, + which the joined tables must have in common, and joins + the tables on equality of those columns. NATURAL is + shorthand for a USING clause that lists all the common + column names of the two tables. A side-effect of both + USING and NATURAL is that only one copy of each joined + column is emitted into the result table (compare the + relational-algebra definition of JOIN, shown earlier). @@ -1144,13 +1146,13 @@ select sname, pname from supplier For each row R1 of T1, the joined table has a row for each row - in T2 that satisfies the join specification with R1. + in T2 that satisfies the join condition with R1. - + The words INNER and OUTER are optional for all JOINs. - INNER is the default. LEFT, RIGHT, and FULL are for - OUTER JOINs only. + INNER is the default. LEFT, RIGHT, and FULL imply an + OUTER JOIN. @@ -1166,9 +1168,9 @@ select sname, pname from supplier First, an INNER JOIN is performed. - Then, where a row in T1 does not satisfy the join specification - with any row in T2, a joined row is returned with null fields in - columns from T2. + Then, for each row in T1 that does not satisfy the join + condition with any row in T2, an additional joined row is + returned with null fields in the columns from T2. @@ -1187,12 +1189,10 @@ select sname, pname from supplier - Rule 1: For each row R2 of T2, the joined table has a row for each - row in T1 that satisfies the join specification with R2 (transposed - [INNER] JOIN). - Rule 2: Where a row in T2 does not satisfy the join specification - with any row in T1, a joined row is returned with null fields in - columns from T1. + First, an INNER JOIN is performed. + Then, for each row in T2 that does not satisfy the join + condition with any row in T1, an additional joined row is + returned with null fields in the columns from T1. @@ -1211,8 +1211,13 @@ select sname, pname from supplier - First, a LEFT [OUTER] JOIN is performed. - Then, Rule 2 of a RIGHT [OUTER] JOIN is performed. + First, an INNER JOIN is performed. + Then, for each row in T1 that does not satisfy the join + condition with any row in T2, an additional joined row is + returned with null fields in the columns from T2. + Also, for each row in T2 that does not satisfy the join + condition with any row in T1, an additional joined row is + returned with null fields in the columns from T1. @@ -1227,40 +1232,15 @@ select sname, pname from supplier - - - NATURAL JOINs - - - - T1 - NATURAL - - INNER - - - LEFT - RIGHT - FULL - - OUTER - - - JOIN - T2 - - - - A natural join creates a joined table where every pair of matching - column names between the two tables are merged into one column. The - join specification is effectively a USING clause containing all the - common column names and is otherwise like a Qualified JOIN. - - - - + + JOINs of all types can be chained together or nested where either or both of + T1 and + T2 may be JOINed tables. + Parenthesis can be used around JOIN clauses to control the order + of JOINs which are otherwise processed left to right. + @@ -1302,7 +1282,7 @@ SELECT AVG(PRICE) AS AVG_PRICE - If we want to know how many parts are stored in table PART we use + If we want to know how many parts are defined in table PART we use the statement: @@ -1332,7 +1312,7 @@ SELECT COUNT(PNO) aggregate operators described above can be applied to the groups --- i.e. the value of the aggregate operator is no longer calculated over all the values of the specified column but over all values of a - group. Thus the aggregate operator is evaluated individually for every + group. Thus the aggregate operator is evaluated separately for every group. @@ -1547,7 +1527,7 @@ SELECT * In our example the result will be empty because every supplier sells at least one part. Note that we use S.SNO from the outer SELECT within the WHERE clause of the inner SELECT. Here the subquery must be - evaluated for every tuple from the outer query, i.e. the value for + evaluated afresh for each tuple from the outer query, i.e. the value for S.SNO is always taken from the current tuple of the outer SELECT. @@ -1605,7 +1585,7 @@ SELECT MAX(subtable.avgprice) SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNAME = 'Jones' - UNION +UNION SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNAME = 'Adams'; @@ -1628,7 +1608,7 @@ gives the result: SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 1 - INTERSECT +INTERSECT SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO < 3; @@ -1652,7 +1632,7 @@ SELECT S.SNO, S.SNAME, S.CITY SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 1 - EXCEPT +EXCEPT SELECT S.SNO, S.SNAME, S.CITY FROM SUPPLIER S WHERE S.SNO > 3;