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 1kk1B7-0002z9-RA for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Dec 2020 08:38:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kk1B6-0000Fk-Oc for pgsql-hackers@arkaria.postgresql.org; Tue, 01 Dec 2020 08:38:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kk1B6-0000Fc-GP for pgsql-hackers@lists.postgresql.org; Tue, 01 Dec 2020 08:38:28 +0000 Received: from mout.kundenserver.de ([217.72.192.75]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kk1B4-0004VT-AS for pgsql-hackers@lists.postgresql.org; Tue, 01 Dec 2020 08:38:28 +0000 Received: from [192.168.178.34] ([77.12.74.114]) by mrelayeu.kundenserver.de (mreue108 [212.227.15.145]) with ESMTPSA (Nemesis) id 1MTzjI-1kam0y27ow-00R56e; Tue, 01 Dec 2020 09:38:22 +0100 Subject: Re: Change JOIN tutorial to focus more on explicit joins To: "David G. Johnston" Cc: Anastasia Lubennikova , PostgreSQL Hackers 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> From: =?UTF-8?Q?J=c3=bcrgen_Purtz?= Message-ID: Date: Tue, 1 Dec 2020 09:38:20 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------52EDB68F99A410E16335D66B" Content-Language: en-US X-Provags-ID: V03:K1:i4iTQ8++Ba508gsijg2tIiCfMRQrYv8Jkf9OCk4nB6soxlhQjqH z1fmGjzIlpedN0pv2co1uZtILJ1eoUrPRqWj/q5cYQnASnyemy9xnpQK0a1wBVBFp6XHnxt Q8e0C4m2185gRllQPhfoxs8jRmv6OW87idZ6LnZ4aCcZKW+4DUev1QiNRhrq5ZaNiVARFjE VuaWzgP23I66ikTWe0rxQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:THIwGPIYrdM=:OSx7TMjxr1w/yVw7ZdUC0X Joh0WXQEJzEd7DMNuhYCo6JRBWhlEkN85wuAk0KPzHo4ncEJThnjSl5pFLib+TCtOY/2yACZW X+z+hqX7DEEwjtM3OOhWuuj9/nW2e7X07tsPwU7MzSTafzHfbbS6CzHApMqmWNtkCTG3kjfy6 MxQi++vv63vRfPUdw3UbvVQnjPBWw2Bc6111A1+BGIIszK55PYM5segLNUOadq0dOyoRFZKcx PBMTL3MFG+3vfp2hd6l+bDkItJ+oMeV3e8pYX+mTc0pASmBnsaanP4kGHKiIDvloLaz/hwThx pHkIXBwUKfJ45IFoXBcDf/xOtkgBe6Yy9FqZx1L6VsUb2bsmHM98cU+MTFDpiTik2Hu3AcXau 263pXoeLnmriYbCFOdaEFr9HI0iH6MBFz6+g32vy05qv5VFDkz19/ewzN+lZQ List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------52EDB68F99A410E16335D66B Content-Type: multipart/alternative; boundary="------------F48462513101B472506A23A2" --------------F48462513101B472506A23A2 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 30.11.20 21:25, David G. Johnston wrote: > Sorry, I managed to overlook the most recent patch. > > I admitted my use of parentheses was incorrect and I don't see anyone > else defending them.  Please remove them. > > Minor typos: > > "the database compare" -> needs an "s" (compares) > > "In this case, the definition how to compare their rows." -> remove, > redundant with the first sentence > > "The results from the older implicit syntax, and the newer explicit > JOIN/ON syntax, are identical" -> move the commas around to what is > shown here > > David J. > > OK. Patch attached. -- J. Purtz --------------F48462513101B472506A23A2 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 30.11.20 21:25, David G. Johnston wrote:
Sorry, I managed to overlook the most recent patch.

I admitted my use of parentheses was incorrect and I don't see anyone else defending them.  Please remove them.

Minor typos:

"the database compare" -> needs an "s" (compares)

"In this case, the definition how to compare their rows." -> remove, redundant with the first sentence

"The results from the older implicit syntax, and the newer explicit JOIN/ON syntax, are identical" -> move the commas around to what is shown here

David J.


OK. Patch attached.

--

J. Purtz

--------------F48462513101B472506A23A2-- --------------52EDB68F99A410E16335D66B Content-Type: text/x-patch; charset=UTF-8; name="0004-query.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0004-query.patch" diff --git a/doc/src/sgml/query.sgml b/doc/src/sgml/query.sgml index e73e805ec4..f343833dae 100644 --- a/doc/src/sgml/query.sgml +++ b/doc/src/sgml/query.sgml @@ -440,13 +440,12 @@ 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 which access multiple tables (including repeats) 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. @@ -461,10 +460,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 ---------------+---------+---------+------+------------+---------------+----------- @@ -499,23 +504,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 @@ -526,8 +522,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 @@ -537,15 +533,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. 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 @@ -558,12 +568,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; @@ -593,10 +604,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 @@ -610,10 +620,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; @@ -630,8 +639,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. --------------52EDB68F99A410E16335D66B--