ITPEC FE Morning April 2022 Question 27
The tables “Flight” and “City” are created as shown below. Which of the following is the SQL to output the flight code, its origin city name, and its destination city name from those tables?
Explanation
City listed twice under the aliases c1 and c2 — the origin and the destination are two different City rows, so each needs its own copy of the table in the join.
Each flight refers to City twice. In one result row the origin name and the destination name come from different City rows, which a single reference to the table cannot supply. Correlation names let FROM list the table twice:
- •
City c1, joined onFlight., soOriginCityID = c1. CityID c1.is the originCityName - •
City c2, joined onFlight., soDestinationCityID = c2. CityID c2.is the destinationCityName
Answer (c)
Why not others:
- •(a) — one
Cityrow must satisfy both conditions, so only flights whose origin and destination are the same city qualify, normally none, and both columns would show the same name. - •(b) — the same single-
Cityjoin, so the same empty result, and it selects the city IDs instead of the city names. - •(d) — aliases
Flighttwice instead ofCity:f1andf2are unrelated, so every flight's origin is paired with every flight's destination, a Cartesian product of wrong combinations.
Key rule: When one row refers to the same table through two foreign keys, list that table twice in FROM under different aliases and join each alias on its own key.
Databases · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.