Skip to content
Official PYQ · 2018

GATE CS 2018 Q12 — Tuple Calculus and SQL

Explain with AI

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?

Choose one option
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 Q1Q_1.
  • Query 2 (LEFT OUTER JOIN): Returns all matching rows (Q1Q_1) plus the unmatched rows from the left table (Book), padded with NULL values for the right table's columns.
    Let this result set be Q2Q_2. Clearly, Q1⊆Q2Q_1 \subseteq Q_2.
  • Query 3 (RIGHT OUTER JOIN): Returns all matching rows (Q1Q_1) plus the unmatched rows from the right table (Stock), padded with NULL values for the left table's columns.
    Let this result set be Q3Q_3. Clearly, Q1⊆Q3Q_1 \subseteq Q_3.
  • 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 Q4Q_4.

By definition of these outer joins, we have:

Q4=Q2∪Q3Q_4 = Q_2 \cup Q_3

This implies:

  • Q1⊆Q4Q_1 \subseteq Q_4
  • Q2⊆Q4Q_2 \subseteq Q_4
  • Q3⊆Q4Q_3 \subseteq Q_4

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.

Published
Report a correction
Keep exploringMore Tuple Calculus and SQL questions