Goal: turn many beer rows into summaries and distinguish missing values from empty input.
Connect to beer_db as the beer_db user. All aggregation exercises use the beers and styles tables.
In the connection command, -U beer_db selects the SQL role and -d beer_db selects the database. Expect the psql welcome message and a beer_db=> prompt; run the following SQL there.
Goal: read counts and numeric summaries without confusing values with output rows.
Count all beers and available IBU measurements, then find the average and range of ABV. Both counts are 400 here: this catalogue has no missing IBU values. The NULL experiment comes later.
COUNT(*) counts rows; COUNT(ibu) counts non-NULL measurements. AVG, MIN, and MAX ignore NULL inputs. Expect one summary row: counts 400/400, average ABV 5.55, minimum 2.81, maximum 11.74. ROUND(..., 2) changes the displayed average.
Goal: count unique style values rather than individual beers.
Count how many distinct beer styles exist in the beers table. Use COUNT(DISTINCT style) — without DISTINCT you would just get the total row count again.
Expect 25 in one summary row, not 25 printed rows. DISTINCT removes repeated style values inside this aggregate.
Goal: aggregate only one style’s input rows.
Find the average, minimum, and maximum ABV for Imperial Stout beers only. The WHERE clause runs before the aggregate — only matching rows are included in the calculation.
Expect 11 Imperial Stouts, average 9.80, and ABV range 8.08–11.74. The one output row summarizes those 11 beers.
Goal: apply minimum and maximum to text values.
Find the first and last beer names under the database’s text ordering (collation). MIN and MAX also work on text; their ordering can depend on locale.
Expect Anniversary Ale and Wild Weizen. These are name-order endpoints, not the oldest or newest beers; the query contains no date ordering.
Goal: verify the arithmetic behind AVG, including an exact comparison.
Use SUM(abv) and COUNT(abv) to reconstruct AVG(abv). The sum 2221.80 is a teaching calculation, not a combined alcohol percentage. Compare the unrounded results before displaying both rounded to two places.
Expect 400 measured beers, both displayed averages 5.55, and exact_match = t. t means true: the unrounded expressions agree; two equal rounded displays alone would not prove that.
Goal: distinguish a missing measurement from no measurements at all.
Inspect aggregate_samples: 10, 20, and one NULL. Show NULL explicitly, then compare COUNT(*) = 3 with COUNT(value) = 2. AVG is 15, not 10. Finally filter out every row: COUNT becomes 0, while SUM and AVG return NULL.
The lab supplies this separate three-row fixture automatically. \pset null makes missing output visible without changing the data. Expect total 30 and measured mean 15, then counts 0/0 and (missing) sums/means for the empty input. An aggregate without grouping still produces one summary row.