Goal: use joins to relate beers to breweries, then observe what happens to unmatched rows.
Connect to beer_db as the beer_db user. All JOIN exercises in this lab use the beers and breweries 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: establish the input size before joining.
Count all beers in the real catalogue. You get one summary row containing 400. Keep that value for the full join count; it is not the number of rows printed by psql.
Goal: read a joined row and identify which table supplies each value.
Join beers to breweries on b.brewery_id = br.id. Show the beer name, style, brewery name, and city. The aliases b and br distinguish the two name columns. LIMIT 5 means “show me five examples”; it does not mean the full join has only five rows.
ON specifies which rows match; AS beer and AS brewery label the output columns. Expect five rows from Amber Brewery in this ordered preview.
Goal: check whether this join loses or multiplies beers.
Run COUNT(*) on the same join, without LIMIT. It returns 400, matching the original table. This is expected: brewery_id is required and references the unique breweries.id. Each beer matches exactly one brewery. The five-row preview and the value 400 describe different things.
Goal: demonstrate an unmatched row that the real catalogue cannot supply.
The setup created a separate, three-row import demo. It does not change the real catalogue. First list join_demo_beers: Mystery Ale has no brewery_id. Then join this demo to join_demo_breweries. Sun Lager and Moon Stout survive; Mystery Ale does not. Finally count the join: three input beers became two matched rows. No LIMIT is involved.
The blank brewery_id is SQL NULL, meaning no brewery is supplied; it cannot satisfy the ON equality. Success is three input rows, two named matches, and matched_rows = 2.
Goal: filter by a brewery property while displaying beer properties.
Return to the real beers and breweries tables. Keep only beers brewed in Czech Republic with a WHERE clause after the join. Show beer, style, ABV, and city. LIMIT 10 displays ten examples of this filtered result.
The predicate uses br.country, so it tests the brewery side of each match. Expect ten displayed rows beginning with Brno beers; ORDER BY controls the preview, not which rows qualify.
Goal: follow two relationships without confusing beers with rating rows.
Join breweries → beers → beer_ratings to build a brewery report. Each beer has five ratings, so COUNT(*) would count ratings. COUNT(DISTINCT b.id) counts beers instead. GROUP BY collects rows per brewery; AVG(r.score) averages their ratings. These aggregate expressions are provided as a preview of Block 1.5. Focus on the two ON relationships. Show the ten highest averages; break ties with the brewery name.
ROUND(..., 2) controls presentation. Expect ten brewery summaries; Royal Brewery leads with eight distinct beers and an average rating of 7.28.