Goal: let subqueries supply values or matching rows, then investigate NULL in exclusions.
Connect to beer_db as the beer_db user. This lab uses 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: use a query result as the threshold instead of copying a number.
First inspect the overall average ABV (5.5545 in this seed). Then find beers above it using a scalar subquery instead of hardcoding the number. LIMIT 10 previews the strongest ten qualifying beers; it is not the number of all qualifying beers.
A scalar subquery produces one value here because AVG aggregates the whole table. ROUND formats the baseline; the predicate uses the unrounded average. Every displayed ABV must exceed 5.5545.
Goal: derive the IPA membership list from the styles table.
Inspect the style names containing IPA, then use that subquery inside IN to find matching beers. The inner query supplies three names; the outer query previews ten beers from the matching set. No hardcoded list is needed.
LIKE '%IPA%' matches names containing IPA; % permits text before and after it. Expect American IPA, Double IPA, and Session IPA, followed by ten beers whose style belongs to that set.
Goal: compare a beer with its own style rather than the whole catalogue.
Compare each beer with the average for its own style. The inner query refers to b.style from the outer query: that reference makes it correlated. Display the average rounded to two decimals, but compare against the full AVG value. Preview ten rows, then count all qualifying beers: 205.
The aliases b and b2 distinguish the current beer from rows used for the style average. Check that displayed beers exceed their style’s full-precision threshold; style_avg is only the rounded label.
Goal: compute a reusable per-style result inside FROM.
The provided GROUP BY query computes one average per style. Join beers to those averages, compare at full precision, and round only the displayed style_avg. Preview ten rows, then check the full count is still 205. This rewrite preserves the result; measuring speed is a separate exercise.
The parenthesized query is a derived table, and AS avgs names it for this statement. GROUP BY style yields one threshold per style; ON selects the threshold for each beer. Expect the same preview as before and count 205.
Goal: test participation without multiplying beers by their appearances.
Use EXISTS to count beers that have at least one festival appearance. The equality fa.beer_id = b.id connects the inner query to the current beer. Each beer is counted once, even if it has several appearances. Compare with IN over the same beer IDs: both counts should be 163.
SELECT 1 is a conventional placeholder inside EXISTS; its value is not counted. The existence test stops caring once a match exists, so several appearances still qualify the beer once.
Goal: isolate how one NULL changes NOT IN.
Exclude Double IPA and Session IPA; the baseline count is 371. Then repeat the same exclusion list with one extra value: NULL. The count becomes zero. Only NULL changed, so the comparison isolates the trap. These are literal lists, not changes to the styles table.
For a style outside the two named exclusions, comparing with NULL yields unknown; the combined NOT IN result is unknown and WHERE discards it. The named styles already fail the exclusion. Expect 371 followed by 0, with no table edits.
Goal: recover the intended exclusion with a matching-row test.
Use the provided VALUES list to give the same two excluded styles and NULL to a NOT EXISTS query. Keep the beer only when no excluded style equals b.style. NULL does not satisfy that equality, so it cannot create a matching row. The count returns to the original 371.
VALUES creates a small query-local table; AS excluded(style_name) names the table and its column. SELECT 1 only tests existence. Expect 371 again: the NULL row has no equality match and cannot poison this test.