Goal: keep unmatched beers visible and find those with no festival appearance.
Connect to beer_db as the beer_db user. This lab uses the beers and festival_appearances 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 two input sizes before interpreting matches.
Before joining, understand the tables. Check how many beers are in the database and how many festival appearances exist. These baseline numbers will make the join results meaningful.
Expect 400 beers and 200 appearance records. An appearance is an event entry, not a distinct beer; one beer can appear more than once.
Goal: see how LEFT JOIN represents both matches and missing matches.
Keep every beer with LEFT JOIN. First tell psql to display NULL as (no match), then preview ten rows. The preview includes both matched and unmatched beers. LIMIT 10 is only a sample; LEFT JOIN preserves every beer in the full result, not necessarily exactly one row per beer.
\pset null changes only psql’s display; it does not store the words (no match) in the database. Expect ten rows containing both festival IDs and that marker. The aliases b and fa identify the beer and appearance sides.
Goal: isolate beers for which the right side found no match.
Now apply the anti-join filter: add WHERE fa.beer_id IS NULL to find beers with no festival appearance. Count them too.
Test fa.beer_id, the right-side key: it is NULL in an unmatched placeholder. Use IS NULL, not = NULL. Expect ten preview rows and a full count of 237; the preview alone does not measure the total.
Goal: express the same missing-appearance question as an existence test.
Write the same "beers that never appeared at a festival" query using NOT EXISTS instead of LEFT JOIN + IS NULL. The count must be identical.
SELECT 1 supplies an arbitrary value: EXISTS cares whether any row qualifies, not what it selects. fa.beer_id = b.id checks the current beer; NOT EXISTS keeps it when no appearance qualifies. Expect the same ten names and total 237 as the anti-join.
Goal: count real appearances while preserving beers with none.
Build a count per beer using the provided GROUP BY query. First sort counts ascending to see five beers with zero appearances. Then sort descending to see five beers with appearances. COUNT(fa.beer_id) ignores the NULL added for an unmatched beer. COUNT(*) would count that placeholder as one. Block 1.5 will unpack GROUP BY; here focus on the zero.
Grouping by b.id keeps two beers with the same name separate. Expect five zero-count rows first, then five positive-count rows, with the largest displayed count 3.