-- The query states: Find the (city,state) pairs which house a branch of every type which is listed in the Branch relation.
-- (((( ^^^^^ ^^^^ ))
-- This is equivalent to: Find cities for which "There does NOT EXIST a branchType that is NOT PRESENT in this City"
-- This leads to the double "NOT EXISTS (NOT EXISTS())" solution to relational devision.::
SELECT * -- city,state
FROM city c
WHERE NOT EXISTS (
-- find a branchtype that is not present in our city
SELECT * FROM Branch b
WHERE NOT EXISTS (
-- same city for this branchtype
SELECT * FROM Branch nx
WHERE nx.Branch_City = c.City_Name AND nx.Branch_State = c.State_Name
AND nx.Branch_Type = b.Branch_Type
)
)
;
Relational division is the term for this type of operation.
BTW: the composite (city,state) primary key for the city
table is only there to confuse you. Normally, you would use a numerical (surrogate) city_id
as a primary key for the city table, and also use that as a foreign key in the branches
table.