Goal: resolve an employee hierarchy and connect shows to their lead actors.
Connect to tv_db as the tv_db user. This lab uses the shows, show_cast, and cast_members tables, plus a demo employees table that was created in the setup step.
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: read the IDs that the self-join will resolve into names.
Inspect the eight demo employees. manager_id points to another row in the same table. Alice (id 1) has NULL because she has no manager. The setup creates this small table in tv_db; it does not modify the baked shows or cast data.
Expect eight rows; Bob’s manager_id = 1 refers to Alice’s id = 1. Alice’s blank field is SQL NULL, not employee zero.
Goal: show each employee beside the person their manager_id references.
Write a self-join: join employees to itself to show each employee's name alongside their manager's name. Use aliases emp for the employee side and mgr for the manager side. Use LEFT JOIN so Alice (the CEO) also appears, with NULL as her manager.
Aliases give the same table two roles; no second employee table is created. \pset null labels missing values only in the client, and NULLS LAST puts them after named managers. Expect all eight employees, including Alice with (no manager).
Goal: follow exactly two manager links from each employee.
Extend the self-join to show three levels: employee, their direct manager, and their manager's manager (grandmanager). This requires two self-joins in sequence.
mgr2.id = mgr.manager_id follows the manager’s link, not the original employee’s link again. Expect eight rows: Hana → Dave → Bob, and Alice with both manager fields missing. This fixed-depth query does not traverse an arbitrary-depth tree.
Goal: understand what one joined row represents.
First inspect the full cast of Apex Predator. Predict the row count: one show has ten cast associations, so the join returns ten show/person rows, not one show row. role_name belongs to the association because the same person can have different roles in different shows.
Then list the lead actor and nationality for all ongoing shows. Keep both filters: s.status = 'ongoing' and sc.role_name = 'Lead Actor'. Expect 12 rows spanning ten nationalities. Without the role filter, the ongoing-show report would have 120 cast rows. Extra rows from a one-to-many join are meaningful relationships, not necessarily duplicates.
These are fictional people. Nationality describes the person; it does not tell you the show's production country. The seed's Lead Actor label is a role category, separate from billing order.
Goal: name the ongoing-show selection while preserving the previous report.
Rewrite the previous query using a CTE for readability. Define a CTE called ongoing_shows that selects just id and title for ongoing shows, then join it to show_cast and cast_members in the main SELECT.
WITH defines a common table expression: a query-local named result, not a permanently created table. os aliases that result. Expect the same 12 ordered show/actor rows; a clearer query does not itself prove a faster execution plan.
Goal: use two CTEs to separate three decisions.
Build ongoing_shows to choose shows, then ongoing_leads to resolve their lead actors. In the final SELECT, filter to nationality = 'British'. Predict which two rows from the previous report will remain: North of Here and The Drift.
The first CTE owns the show-status filter, the second owns the association-role filter, and the final query owns the report's nationality filter. A later report could reuse this structure with a different nationality. A CTE only exists for this statement; execute its body as a standalone SELECT when inspecting an intermediate result.
Try replacing British with French afterward: only Darkwood should remain. This changes the requested subset; the previous CTE refactor deliberately preserved all 12 rows.