Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lLiFm-0003sb-EM for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Mar 2021 08:07:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lLiFl-0007Wy-DK for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Mar 2021 08:07:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lLiFl-0007Wr-21 for pgsql-hackers@lists.postgresql.org; Mon, 15 Mar 2021 08:07:05 +0000 Received: from mout.kundenserver.de ([217.72.192.75]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lLiFh-0004p5-PO for pgsql-hackers@lists.postgresql.org; Mon, 15 Mar 2021 08:07:04 +0000 Received: from [192.168.178.34] ([77.189.35.143]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.145]) with ESMTPSA (Nemesis) id 1N64FC-1loXht3NJQ-016RzA; Mon, 15 Mar 2021 09:06:53 +0100 Subject: Re: Change JOIN tutorial to focus more on explicit joins To: Thomas Munro , David Steele Cc: Peter Eisentraut , PostgreSQL Hackers , "David G. Johnston" References: <158996922318.7035.10603922579567326239@wrigleys.postgresql.org> <81d5be9d-aa6f-f181-bea9-5a852b876cad@purtz.de> <160676553158.7563.8055833766353450366.pgcf@coridan.postgresql.org> <9a3ce766-81e9-aa13-3cf3-793e52375df8@purtz.de> <81898782-7e51-30d1-b22b-09809085f534@pgmasters.net> From: =?UTF-8?Q?J=c3=bcrgen_Purtz?= Message-ID: Date: Mon, 15 Mar 2021 09:06:51 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------4A4CC28F33000B84479559D8" Content-Language: en-US X-Provags-ID: V03:K1:S43AzSrVahYTogZWf7j7ZG+YNOuMc6+HqFJEUMTOvO9Dh8ksvF5 O2DBCsK4RQqqkm2BkC754WX5Q4u80TQWGuz9vmWuFRf5MJTYSvE2X/GZfXPNx+arSDv90VW PISrYJRY8rdIP84eIwwjkbP7LEhd3LPeUfIJW9KJfrJounsn5579WMBDSiBRomZ/v05OXqP cZ6KQ7i2JSLwBC2xLrmng== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:3H+1RhQ9Cn4=:phSTTdg68SpMbsq9raM5Bg APceVsJzG4aSxUWQf0SmM10QAPSZVuZ9WPCOZgmSFyO+WDVm1ihM02vRwkOAoL4egpMqGl4xH kxzDcScqFB+XPn8GkDpDHs3uPzJz17K8vSdnaMwC/L6nR8rKubUvzJ5HUVku/ZcXemnozvU+/ xM+OpHK41QlPIFwN9b3NIOuraowCqX9E+yXc2TGySaXF3nlGm5XEBdQFSF7qHqFxFrQxfSNx/ L7Uw5wEHbgCB3JMaI1BeTmNKAS/dIp9B5wfLEziVtRR7Qxa2FOllGbFowbqNtFBiy82j8digH LTdEsmKVBiCiWAEBVuoXuQobhasCtIBEhI9tTiqj6gCsB8P+7HjNqx4vPYms1wXwzd63g7otc uZpKXquBePZ2s8V336YHrSj75sNwPssgwc3rDGXAc/7wQaXV2WsV1/qtndis/ List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------4A4CC28F33000B84479559D8 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 15.03.21 03:47, Thomas Munro wrote: > On Thu, Mar 11, 2021 at 2:06 AM David Steele wrote: >> On 12/1/20 3:38 AM, Jürgen Purtz wrote: >>> OK. Patch attached. > + Queries which access multiple tables (including repeats) at once are called > > I'd write "Queries that" here (that's is a transatlantic difference in > usage; I try to proofread these things in American mode for > consistency with the rest of the language in this project, which I > probably don't entirely succeed at but this one I've learned...). > > Maybe instead of "(including repeats)" it could say "(or multiple > instances of the same table)"? > > + For example, to return all the weather records together with the > location of the > + associated city, the database compares the city > column of each row of the weather table with the > name column of all rows in the > cities > table, and select the pairs of rows where these values match. > > Here "select" should agree with "the database" and take an -s, no? > > + This syntax pre-dates the JOIN and ON > + keywords. The tables are simply listed in the FROM, > + comma-separated, and the comparison expression added to the > + WHERE clause. > > Could we mention SQL92 somewhere? Like maybe "This syntax pre-dates > the JOIN and ON keywords, which were introduced by SQL-92". (That's a > "non-restrictive which", I think the clue is the comma?) +1. All proposed changes integrated. -- Kind regards, Jürgen Purtz --------------4A4CC28F33000B84479559D8 Content-Type: text/x-patch; charset=UTF-8; name="0005-query.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0005-query.patch" diff --git a/doc/src/sgml/query.sgml b/doc/src/sgml/query.sgml index e793398bb2..44aa8ef32b 100644 --- a/doc/src/sgml/query.sgml +++ b/doc/src/sgml/query.sgml @@ -438,16 +438,15 @@ SELECT DISTINCT city Thus far, our queries have only accessed one table at a time. - Queries can access multiple tables at once, or access the same - table in such a way that multiple rows of the table are being - processed at the same time. A query that accesses multiple rows - of the same or different tables at one time is called a - join query. As an example, say you wish to - list all the weather records together with the location of the - associated city. To do that, we need to compare the city + Queries that access multiple tables (or multiple instances of the same table) at once are called + join queries. They internally combine + each row from one table with each row of a second table. An expression is + specified to limit which pairs of rows are returned. + For example, to return all the weather records together with the location of the + associated city, the database compares the city column of each row of the weather table with the name column of all rows in the cities - table, and select the pairs of rows where these values match. + table, and selects the pairs of rows where these values match. This is only a conceptual model. The join is usually performed @@ -459,10 +458,16 @@ SELECT DISTINCT city SELECT * - FROM weather, cities - WHERE city = name; + FROM weather + JOIN cities ON city = name; + After the keyword ON follows the + expression comparing their rows. In this example the + column city of table weather + must be equal to the column name + of table cities. + city | temp_lo | temp_hi | prcp | date | name | location ---------------+---------+---------+------+------------+---------------+----------- @@ -497,23 +502,14 @@ SELECT * *: SELECT city, temp_lo, temp_hi, prcp, date, location - FROM weather, cities - WHERE city = name; + FROM weather + JOIN cities ON city = name; - - Exercise: - - - Attempt to determine the semantics of this query when the - WHERE clause is omitted. - - - Since the columns all had different names, the parser automatically found which table they belong to. If there @@ -524,8 +520,8 @@ SELECT city, temp_lo, temp_hi, prcp, date, location SELECT weather.city, weather.temp_lo, weather.temp_hi, weather.prcp, weather.date, cities.location - FROM weather, cities - WHERE cities.name = weather.city; + FROM weather + JOIN cities ON cities.name = weather.city; It is widely considered good style to qualify all column names @@ -535,15 +531,29 @@ SELECT weather.city, weather.temp_lo, weather.temp_hi, Join queries of the kind seen thus far can also be written in this - alternative form: + form: SELECT * - FROM weather INNER JOIN cities ON (weather.city = cities.name); + FROM weather, cities + WHERE city = name; - This syntax is not as commonly used as the one above, but we show - it here to help you understand the following topics. + This syntax pre-dates the JOIN and ON + keywords, which were introduced by SQL-92. The tables are simply listed + in the FROM, comma-separated, and the comparison + expression added to the WHERE clause. + + + + As join expressions serve a specific + purpose in a multi-table query it is preferable to make them stand-out + by using join clauses to introduce additional tables into the query. + The results from the older implicit syntax and the newer explicit + JOIN/ON syntax are identical. But for a reader of the statement + its meaning is now easier to understand: the join condition is + introduced by its own key word whereas previously the condition was + merged into the WHERE clause together with other conditions. joinouter @@ -556,12 +566,13 @@ SELECT * found we want some empty values to be substituted for the cities table's columns. This kind of query is called an outer join. (The - joins we have seen so far are inner joins.) The command looks - like this: + joins we have seen so far are inner joins.) + The command looks like this: SELECT * - FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name); + FROM weather + LEFT OUTER JOIN cities ON city = name; @@ -591,10 +602,9 @@ SELECT * + joinself + aliasfor table name in query - joinself - aliasfor table name in query - We can also join a table against itself. This is called a self join. As an example, suppose we wish to find all the weather records that are in the temperature range @@ -608,10 +618,9 @@ SELECT * SELECT w1.city, w1.temp_lo AS low, w1.temp_hi AS high, - w2.city, w2.temp_lo AS low, w2.temp_hi AS high - FROM weather w1, weather w2 - WHERE w1.temp_lo < w2.temp_lo - AND w1.temp_hi > w2.temp_hi; + w2.city, w2.temp_lo AS low, w2.temp_hi AS high + FROM weather w1 + JOIN weather w2 ON w1.temp_lo < w2.temp_lo AND w1.temp_hi > w2.temp_hi; @@ -628,8 +637,8 @@ SELECT w1.city, w1.temp_lo AS low, w1.temp_hi AS high, queries to save some typing, e.g.: SELECT * - FROM weather w, cities c - WHERE w.city = c.name; + FROM weather w + JOIN cities c ON w.city = c.name; You will encounter this style of abbreviating quite frequently. --------------4A4CC28F33000B84479559D8--