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 1kE89J-0004Ej-2b for pgsql-docs@arkaria.postgresql.org; Fri, 04 Sep 2020 09:36:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kE89I-0002A3-0h for pgsql-docs@arkaria.postgresql.org; Fri, 04 Sep 2020 09:36:48 +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 1kE89H-00029v-Pq for pgsql-docs@lists.postgresql.org; Fri, 04 Sep 2020 09:36:47 +0000 Received: from mout.kundenserver.de ([212.227.126.131]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kE89F-0001lo-4T for pgsql-docs@lists.postgresql.org; Fri, 04 Sep 2020 09:36:47 +0000 Received: from [192.168.178.43] ([77.189.247.184]) by mrelayeu.kundenserver.de (mreue009 [212.227.15.129]) with ESMTPSA (Nemesis) id 1M597s-1kFEbu16jE-001Aku; Fri, 04 Sep 2020 11:36:42 +0200 Subject: Re: Change JOIN tutorial to focus more on explicit joins To: Peter Eisentraut , Thomas Munro , shammat@gmx.net, pgsql-docs@lists.postgresql.org References: <158996922318.7035.10603922579567326239@wrigleys.postgresql.org> <9fd4b399-37c8-014e-e7ef-08fb0ecbbdcc@2ndquadrant.com> From: =?UTF-8?Q?J=c3=bcrgen_Purtz?= Message-ID: <791cac8a-e5f7-a79c-d651-dfa3ecc1453e@purtz.de> Date: Fri, 4 Sep 2020 11:36:39 +0200 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: <9fd4b399-37c8-014e-e7ef-08fb0ecbbdcc@2ndquadrant.com> Content-Type: multipart/mixed; boundary="------------83A18EEB87F7DCCCBD572634" Content-Language: en-US X-Provags-ID: V03:K1:rFzXo2MVb24W+RQkVBddMDLMnOD08F1xfE5I5o6EDA8NTEzUU9y TLrWElenr+bmbWPaxShsYfk1d7+MTp6B7XdS79VbWSomhtf154HxovWrJt07DI5RZu+AhbV SlyXfjuLbl7duWYnewKqMwMvykK3R02fzH0opySld2uIZFAa2clCy4hFOjRbV0m6J+iEYNL ZiPLPF21AY4DudHRbx77Q== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:WnxHlpiiL38=:kSzzEA+tF+xSWMQ7w8B8yR h8XrBZm8sL60GYW+B+xt/Fyv9zWlgIWz2e3eVmzwRUhEyuMfDcH9alFqLqdcBqNeOYyZTpqAU 3qYIJzQL/XV+vx6e/39Azb5d3P0xbwFWt4Eg6NQA6bBbEaFJ8thGFynJ/RjG0Rx3YHjhEJJmQ 7GY/WZieas+qEx4G9aJ2PC8zn4XnEhTmvs+LR2jsw5twTXO9O8dwq7vdIt2rkNUf+FJOI8kTe 597cxLCPERoUDP7Cmb/KMYbdDC6OpzBF36RWakkkGIvHlbTjZoogQRElNV24MnabuG/3OcDri b1kAWNP3V5yk+YrePcbiHwmjQNo/Pdbe/1xnmEs5/Spsmyz/w/yuCnpypueKgDe0PRTXelyjM uLHSpz8A6HYG110lO4m+RsDY2FEaqVakkTR2t1dk33elTL0Y/EFtIYMC3IGWLedm4KIVhCIev gPrPZgoT8Jeou4VtaL5DUxPC9Qm6kuS+4keXkdFhLAMMjQDbY52zGnFAnFajU228JIw/lhmyJ uP5pIKQutymG9ovVx0RhpJ3ZoBA7veX5kqxtKGOfDrU+4IgdbvfhcyTtm+KKZzMBIUadFEmDG mO7YGqpQs/p1Dnp64xKhZbUS+Q/FsUwzTo8wvlb42Waup7MFj974BHm+6uCywR+3E92kkuE3J ZCJIo9uVrOfQMlJuMxiBNAdYOTS6ItvsD0CHuYArHO9Pzgnikoe+Ce4jxIDTiF5tEi5piZXUj 3RnsqIXXueQrC2xkuVctbWaZTPWgWkbYRseAB6rdyCFQuer5iGDOO2BUn3R2mil5FZFK6Vyd0 uquhlJ6j8aW0PA9HZgHdrH15AB6Xal1GBlkEhAXRp6M9KhbPXVXiNlMlGghk5tG9PHEDEEG List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------83A18EEB87F7DCCCBD572634 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 04.09.20 08:52, Peter Eisentraut wrote: > > For the remaining patch I have a couple of concerns: > > >      > >       Attempt to determine the semantics of this query when the > > -     WHERE clause is omitted. > > +     ON clause is omitted. > >      > >     > > This no longer works. > Ok, but I don't have any better suggestion than to delete this para. > In general, I agree that some more emphasis on the JOIN syntax is > okay. But I think the order in which the tutorial has taught it so far > is okay: First you do it the manual way, then you learn the more > abstract way. In this context, I wouldn't use the terms 'manual' and 'abstract', it's more about 'implicit' and 'explicit' syntax. The 'explicit' syntax does not only emphasis the aspect of 'joining' tables, it also differentiates between the usage of following AND/OR/NOT key words as join conditions or as additional restrictions (the results are identical but not the semantic). Because the purpose of this patch is the preference of the explicit syntax, we shall show this syntax first. > > > +   > > +    > > +     The examples shown here combine rows via city names. > > +     This should help to understand the concept. Professional > > +     solutions prefer to use numerical IDs and foreign keys > > +     to join tables. > > +    > > +   > > While there are interesting debates to be had about natural vs. > surrogate keys, I don't think we should imply that one of them is > unprofessional and then leave it at that and give no further guidance. > I think we should leave this out. > Ok, deleted. -- Jürgen Purtz --------------83A18EEB87F7DCCCBD572634 Content-Type: text/x-patch; charset=UTF-8; name="0002-query.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0002-query.patch" diff --git a/doc/src/sgml/query.sgml b/doc/src/sgml/query.sgml index e73e805ec4..413763691e 100644 --- a/doc/src/sgml/query.sgml +++ b/doc/src/sgml/query.sgml @@ -441,11 +441,11 @@ 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 + table several times. Such queries — they are called + join queries — combine + rows of one table in some way with rows of the other table + and return a single row per combination. An example may be a + list of all the weather records together with the location of the associated city. To do that, we need to compare the city column of each row of the weather table with the name column of all rows in the cities @@ -461,10 +461,17 @@ SELECT DISTINCT city SELECT * - FROM weather, cities - WHERE city = name; +FROM weather +JOIN cities ON (city = name); + The keyword JOIN connects the two tables. + Behind the keyword ON follows the + definition how to compare their rows. In this case, 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 ---------------+---------+---------+------+------------+---------------+----------- @@ -476,8 +483,14 @@ SELECT * - Observe two things about the result set: + Observe some things about the result set: + + + The resulting rows contain values from both tables. + + + There is no result row for the city of Hayward. This is @@ -499,23 +512,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 +530,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 +541,18 @@ 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 weather.city = cities.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 is mainly used in legacy applications. It dates back + to the first days of SQL, avoids the JOIN + keyword, and uses the WHERE clause instead of the + ON clause. joinouter @@ -558,7 +565,7 @@ 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 + joins we have seen so far are inner joins.) The command looks like this: @@ -593,10 +600,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 +616,10 @@ 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 +636,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. --------------83A18EEB87F7DCCCBD572634--