Goal: synchronize ratings, verify a next-page cursor, and import a real CSV.
Connect as tv_db. Rating writes use the separate ratings_sync fixture; episodes supplies the pagination data.
In the connection command, -U tv_db selects the SQL role and -d tv_db selects the database. Expect the psql welcome message and a tv_db=> prompt; run the following SQL there.
Goal: take both the insert and update paths of an upsert.
Run the upsert twice on the same unique (source, external_id) pair. First it inserts score 8.50 and 1200 votes. Then it updates that row to 8.80 and 1300 votes. Both commands report INSERT 0 1; the changed values distinguish the two paths.
The lab creates ratings_sync separately from baked imports. Its unique pair identifies one rating. EXCLUDED refers to the proposed new row, so EXCLUDED.score supplies the incoming score. RETURNING exposes the stored values; the first episode subquery provides an existing episode ID. Success requires both value sets in order, not just the identical command tags.
Goal: merge one matching and one new source row.
Sync two source rows into ratings_sync. tt0000001 already exists: update it to 9.10 / 1500 votes. tt0000002 is new: insert it as 7.40 / 800 votes. MERGE 2 counts both actions; SELECT shows which values survived.
USING (VALUES ...) is a two-row source; its alias column names let ON compare both identity fields. WHEN MATCHED updates the existing target and WHEN NOT MATCHED inserts the new one. now() records the update time. Expect exactly the two external IDs with their stated scores/votes.
Goal: see how LIMIT and OFFSET choose pages from a stable order.
Fetch page 1 and page 5 with five rows per page. OFFSET 20 skips the first 20 ordered results before returning five. These queries demonstrate page positions, not a timing benchmark. Sort by air_date AND id so tied dates have a stable order.
Page 5 starts after (5 - 1) × 5 = 20 rows. Expect five rows per query, beginning on 2010-01-01 and 2010-06-30 respectively. IS NOT NULL excludes undated episodes before ordering; the same rule is used for the cursor comparison next.
Goal: request the next page by its last ordering key and verify membership.
Fetch the first five episodes. Its last row is dated 2010-01-29, ID 2a414d88-69cc-2f5f-9fce-5a96743a0885. Use that actual pair as the next-page cursor. The second query has no OFFSET. Finally compare its IDs with OFFSET 5: five rows each and zero differences. The baked dates are all unique; Learn includes a tiny tied-date counterexample to explain the ID tie-breaker. The supplied WITH/EXCEPT check reuses Block 1.4 ideas; it verifies the sets, not speed.
(air_date, id) > (...) compares pairs in order: date first, then ID for tied dates. ::date and ::uuid give the supplied literals the correct SQL types. Each EXCEPT finds IDs missing from the other page; UNION ALL combines both directions. Expect cursor_rows = 5, offset_rows = 5, and differing_ids = 0; that zero is the membership check.
Goal: switch to the database and owner needed for the CSV write.
Leave tv_db with \q, then connect as social_db. The CSV exercise uses the separate moderation_import fixture.
\q returns to the shell; run the new psql command there. -U names the SQL role and -d the database. Expect the social_db=> prompt; this is a new connection, not a SQL query against tv_db.
Goal: verify the file contents and import its three data records.
Inspect the supplied CSV with \! cat. Use psql’s \copy to send it through COPY FROM STDIN into moderation_import. HEADER skips the header line. Expect COPY 3, then SELECT the three imported rows. Run the import once; reset the lesson if you want a fresh start.
\! runs cat in a shell and returns to psql. The lab supplies /home/student/mod-actions.csv; no download or file creation is needed. The destination column list maps CSV fields by position. COPY 3 counts imported records, excluding the header. The final ordered query must show comment/warn, post/remove, and user/ban with their reasons.