Doc: sync src/tutorial/basics.source with SGML documentation.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 19 Nov 2022 18:09:14 +0000 (13:09 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 19 Nov 2022 18:09:14 +0000 (13:09 -0500)
basics.source is supposed to be pretty closely in step with
the examples in chapter 2 of the tutorial, but I forgot to
update it in commit f05a5e000.  Fix that, and adjust a couple
of other discrepancies that had crept in over time.

(I notice that advanced.source is nowhere near being in sync
with chapter 3, but I lack the ambition to do something
about that right now.)

src/tutorial/basics.source

index 3e74d718ab0057a8b6a66bebf4c5012a4230ea09..d09ff5029bcae45b75bc7888e37343380a75c1df 100644 (file)
@@ -79,6 +79,11 @@ SELECT *
     WHERE city = 'San Francisco'
         AND prcp > 0.0;
 
+-- You can request that the results of a query be returned in sorted order:
+
+SELECT * FROM weather
+    ORDER BY city, temp_lo;
+
 -- Here is a more complicated one.  Duplicates are removed when DISTINCT is
 -- specified. ORDER BY specifies the column to sort on.  (Just to make sure the
 -- following won't confuse you, DISTINCT and ORDER BY can be used separately.)
@@ -108,7 +113,8 @@ SELECT city, temp_lo, temp_hi, prcp, date, location
 -- table name. If you want to be clear, you can do the following. They give
 -- identical results, of course.
 
-SELECT weather.city, weather.temp_lo, weather.temp_hi, weather.prcp, weather.date, cities.location
+SELECT weather.city, weather.temp_lo, weather.temp_hi,
+       weather.prcp, weather.date, cities.location
     FROM weather JOIN cities ON weather.city = cities.name;
 
 -- Old join syntax
@@ -125,8 +131,8 @@ SELECT *
 -- Suppose we want to find all the records that are in the temperature range
 -- of other records.  w1 and w2 are aliases for weather.
 
-SELECT w1.city, w1.temp_lo, w1.temp_hi,
-       w2.city, w2.temp_lo, w2.temp_hi
+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 JOIN weather w2
     ON w1.temp_lo < w2.temp_lo AND w1.temp_hi > w2.temp_hi;
 
@@ -142,16 +148,27 @@ SELECT city FROM weather
     WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
 
 -- Aggregate with GROUP BY
-SELECT city, max(temp_lo)
+SELECT city, count(*), max(temp_lo)
     FROM weather
     GROUP BY city;
 
 -- ... and HAVING
-SELECT city, max(temp_lo)
+SELECT city, count(*), max(temp_lo)
     FROM weather
     GROUP BY city
     HAVING max(temp_lo) < 40;
 
+-- We can filter rows before aggregating them:
+SELECT city, count(*), max(temp_lo)
+    FROM weather
+    WHERE city LIKE 'S%'
+    GROUP BY city;
+
+-- Another way is the FILTER clause, which operates per-aggregate:
+SELECT city, count(*) FILTER (WHERE temp_lo < 45), max(temp_lo)
+    FROM weather
+    GROUP BY city;
+
 
 -----------------------------
 -- Updates: