Consider the following two tables and four queries in SQL.
Book (isbn, bname), Stock(isbn, copies)
Query 1:
SELECT B.isbn, S.copies FROM Book B INNER JOIN Stock S ON B.isbn=S.isbn;
Query 2:
SELECT B.isbn, S.copies FROM Book B LEFT OUTER JOIN Stock S ON B.isbn=S.isbn;
Query 3:
SELECT B.isbn, S,copies FROM Book B RIGHT OUTER JOIN Stock S ON B.isbn=S.isbn
Query 4:
SELECT B.isbn, S.copies FROM Book B FULL OUTER JOIN Stock S ON B.isbn=S.isbn
Which one of the queries above is certain to have an output that is a superset of the outputs of the other three queries?
View worked solution
Answer: D
To determine which query's output is guaranteed to be a superset of the others, let us analyze the behavior of the four SQL joins on the tables Book (B) and Stock (S) under the join condition B.isbn = S.isbn:
- Query 1 (INNER JOIN): Returns only the rows where there is a match in both tables.
Let this result set be . - Query 2 (LEFT OUTER JOIN): Returns all matching rows () plus the unmatched rows from the left table (
Book), padded withNULLvalues for the right table's columns.
Let this result set be . Clearly, . - Query 3 (RIGHT OUTER JOIN): Returns all matching rows () plus the unmatched rows from the right table (
Stock), padded withNULLvalues for the left table's columns.
Let this result set be . Clearly, . - Query 4 (FULL OUTER JOIN): Returns all matching rows, all unmatched rows from the left table, and all unmatched rows from the right table.
Let this result set be .
By definition of these outer joins, we have:
This implies:
Thus, the output of Query 4 is always a superset of the outputs of the other three queries.
Free practice · Your answers on this page are not saved.
